Kamis, 05 Maret 2015

How to Open, Read, Close, and Write file in PHP


This article explains how to manipulate file in PHP
PHP Open File - fopen()
Using fopen() function to open files which allows you to gives more options compared readfile() function. "webdictionary.txt", is used aas text for the example below d
The 1st lines of fopen() have the files of name to be opened and the second lines is to specifies which type of the files to be opened. The following example also teach how to create and also generate a sentence of messages:
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>
For modes Desciptions try and search the internet for more information.
PHP Read File
The fread() is used as a function to reads from an open file.The 1st line of fread() have the file title which are to be read from and the 2nd line specifies the maximum number of bytes to read.
fread($myfile,filesize("webdictionary.txt"));
PHP Close File - fclose()
The fclose() function is use to close an existing or opened file.The fclose() conditions is the file name or the variable of the file name which we want to close:
<?php
$myfile = fopen("webdictionary.txt", "r");
// some code to be executed....
fclose($myfile);
?>
PHP Read Single Line - fgets()
The fgets() function as the name suggest is used to red a single line.
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
fclose($myfile);
?>
PHP Create File - fopen()
The fopen() function is not to just open a file but instead it is use also to create a new file.

Tidak ada komentar:

Posting Komentar