Page Stats
Visitor: 349
File handling is an important part of any web application. You often need to open and process a file for different tasks. PHP has several functions for reading, writing, editing, updating files.
The readfile() function reads a file and writes it to the output buffer.
Assume we have a text file called "web.txt", that looks like this:
HTML = Hyper Text Markup Language CSS = Cascading Style Sheets PHP = Hypertext Preprocessor SQL = Structured Query LanguageExample: The PHP code to read the file.
<?php echo readfile("web.txt"); ?>
A better method to open files is with the fopen() function. This function gives you more options than the readfile() function. The first parameter of fopen() contains the name of the file to be opened and the second parameter specifies in which mode the file should be opened.
$myfile = fopen("web.txt", "r") or die("Unable to open file!");
The fread() function reads text from the file. The first parameter of fread() function is the name of the file to read from and the second parameter specifies the maximum number of bytes to read.
The following PHP code reads the "web.txt" file to the end:fread($myfile,filesize("web.txt"));
PHP Close File - The fclose() function is used to close an open file.
fclose($myfile);
Example: The PHP code to open, read and close the file.
<?php $myfile = fopen("web.txt", "r") or die("Unable to open file!"); echo fread($myfile,filesize("web.txt")); fclose($myfile); ?>
Check whether the file exists or not. echo file_exists("web.txt"));
The fgets() function is used to read a single line from a file.
<?php $myfile = fopen("web.txt", "r") or die ("Unable to open file!"); echo fgets($myfile); fclose($myfile); ?>
Check End-Of-File - The feof() function checks if the end-of-file
(EOF) has been reached.
<?php $myfile = fopen("web.txt", "r") or die("Unable to open file!"); while(!feof($myfile)) { echo fgets($myfile) . "<br>"; } fclose($myfile); ?>
The fgetc() function is used to read a single character from a file.
<?php $myfile = fopen("web.txt", "r") or die("Unable to open file!"); while(!feof($myfile)) { echo fgetc($myfile); } fclose($myfile); ?>
The file may be opened in one of the following modes:
Modes | Description |
---|---|
r | Open a file for reading only. |
w | Open a file for writing only. Erases the contents of the file or creates a new file if it doesn't exist. |
a | Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist |
x | Creates a new file for write only. Returns FALSE and an error if file already exists |
The fopen() function is also used to create a file. If you use fopen() on a file that does not exist, it will create it, the file is opened for writing (w) or appending (a).
$myfile = fopen("testfile.txt", "w");
PHP Write to File - fwrite()
The fwrite() function is used to write to a file. The first parameter of fwrite() contains filename in which you want to write and the second parameter is the string to be written.
<?php $myfile = fopen("newfile.txt", "w") or die("Unable to open file!"); $txt = "Tim Berners Lee\n"; fwrite($myfile, $txt); $txt = "Hakon Wium Lie\n"; fwrite($myfile, $txt); fclose($myfile); ?>