PHP File Open/Read/Close
- Previous Page PHP File
- Next Page PHP File Creation/Write
In this section, we will explain how to open, read, and close files on the server.
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 this course, we will use the text file "webdictionary.txt":
AJAX = Asynchronous JavaScript and XML CSS = Cascading Style Sheets HTML = Hyper Text Markup Language PHP = PHP Hypertext Preprocessor SQL = Structured Query Language SVG = Scalable Vector Graphics XML = EXtensible Markup Language
The first parameter of fopen() contains the name of the file to be opened, and the second parameter specifies the mode to open the file. If the fopen() function fails to open the specified file, the following example will generate a message:
Example
<?php $myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!"); echo fread($myfile, filesize("webdictionary.txt")); fclose($myfile); ?>
Tip:We will learn about the fread() and fclose() functions next.
The file will be opened in one of the following modes:
Mode | Description |
---|---|
r | Open the file for read-only. The file pointer starts at the beginning of the file. |
w | Open the file for write-only. Delete the content of the file or create a new file if it does not exist. The file pointer starts at the beginning of the file. |
a | Open the file for write-only. The existing data in the file will be retained. The file pointer starts at the end of the file. Create a new file if it does not exist. |
x | Create a new file for write-only. Returns FALSE and an error if the file already exists. |
r+ | Open the file for read/write with the file pointer at the beginning. |
w+ | Open the file for read/write. Delete the file content or create a new file if it does not exist. The file pointer starts at the beginning of the file. |
a+ | Open the file for read/write. The existing data in the file will be retained. The file pointer starts at the end of the file. Create a new file if it does not exist. |
x+ | Create a new file for read/write. Returns FALSE and an error if the file already exists. |
PHP Read File - fread()
The fread() function reads the opened file.
The first parameter of fread() contains the 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()
fclose() function is used to close the opened file.
Note:It is a good programming habit to close all files after use. You don't want files that you don't want to open to consume your server resources.
fclose() requires the name of the file to be closed (or a variable containing the file name):
<?php $myfile = fopen("webdictionary.txt", "r"); // some code to be executed.... fclose($myfile); ?>
PHP Reading a 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 $myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!"); echo fgets($myfile); fclose($myfile); ?>
Note:After calling the fgets() function, the file pointer moves to the next line.
PHP Check End-Of-File - feof()
The feof() function checks if it has reached the "end-of-file" (EOF).
feof() is very useful for traversing data of unknown length.
The following example reads the "webdictionary.txt" file line by line until end-of-file:
Example
<?php $myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!"); // Output single line until end-of-file while(!feof($myfile)) { echo fgets($myfile) . "<br>"; } fclose($myfile); ?>
PHP Read Single Character - fgetc()
The fgetc() function is used to read a single character from the file.
The following example reads the "webdictionary.txt" file character by character until end-of-file:
Example
<?php $myfile = 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 Filesystem reference manual, please visit the one provided by CodeW3C.com PHP Filesystem Reference Manual.
- Previous Page PHP File
- Next Page PHP File Creation/Write