PHP Open file-fopen ()
A better way to open a file is through the fopen () function. This function gives you more options than the ReadFile () function.
In the course, we will use the text file "Webdictionary.txt":
AJAX ==== = = = ExtensibleMarkup Language
The first parameter of fopen () contains the file name that is opened, and the second parameter specifies the mode in which the file is opened. If the fopen () function fails to open the specified file, the following example generates a message:
Example
<? PHP = fopen ("webdictionary.txt""R") or Die (" Unable to open file! " ); Echo fread ($myfile, FileSize ("webdictionary.txt")); Fclose ($myfile);? >
Tip: We will then learn fread () and the fclose () function.
The file is opened in one of the following modes:
PHP Read Files-fread ()
The Fread () function reads the open file.
The first parameter of Fread () contains the file name of the file to be read, and the second parameter specifies the maximum number of bytes to be read.
The following PHP code reads the "webdictionary.txt" file to the end:
Fread ($myfile, FileSize ("webdictionary.txt"));
PHP Close File-fclose ()
The fclose () function is used to close open files.
Note: It is a good programming habit to close all of the files after they are exhausted. You do not want to open a file that consumes your server resources.
Fclose () requires the name of the file to be closed (or a variable that has a file name):
<? PHP = fopen ("webdictionary.txt""R"); // some code to be executed .... fclose ($myfile);? >
PHP read single-line file-fgets ()
The fgets () function is used to read a single line from a file.
The following example outputs the first line of the "Webdictionary.txt" file:
Example
<? PHP = fopen ("webdictionary.txt""R") or Die ("Unable To open file! " ); Echo fgets ($myfile); Fclose ($myfile);? >
Note: After you call the Fgets () function, the file pointer moves to the next line.
PHP Check end-of-file-feof ()
The feof () function checks to see if "End-of-file" (EOF) has been reached.
Feof () is useful for traversing data with unknown lengths.
The following example reads the "Webdictionary.txt" file row by line until End-of-file:
Example
<? PHP = fopen ("webdictionary.txt""R") or Die ("Unable To open file! " ); // output single line until End-of-file while (! feof ($myfile)) { "<br>"; } Fclose ($myfile);? >
PHP Read single character-fgetc ()
The fgetc () function is used to read a single character from a file.
The following example reads "Webdictionary.txt" files verbatim until End-of-file:
Example
<? PHP = fopen ("webdictionary.txt""R") or Die ("Unable To open file! " ); // output single character until End-of-file while (! feof ($myfile)) { echo fgetc ($myfile); } Fclose ($myfile);? >
Note: After calling the Fgetc () function, the file pointer moves to the next character.
PHP Filesystem Reference manual
For a complete PHP file System Reference Manual, please visit the PHP Filesystem reference manual provided by W3school.
PHP File Open/Read