PHP File Open/Read/Close
PHP fopen() - Open a file
The fopen() function is used to open a file or a URL.
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.
| Mode | Description |
|---|---|
| r | Read only. File pointer starts at the beginning of the file |
| r+ | Read/Write. File pointer starts at the beginning of the file. Existing data is preserved |
| w | Write only. Erases the contents of the file, or creates a new file if it doesn't exist. File pointer starts at the beginning of the file |
| w+ | Read/Write. Erases the contents of the file, or creates a new file if it doesn't exist. File pointer starts at the beginning of the file |
| a | Append (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 |
| a+ | Append (read/write). 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 | Write only. Creates a new file. Returns FALSE and an error if file already exists |
| x+ | Read/Write. Creates a new file. Returns FALSE and an error if file already exists |
We will use the text file, "webdictionary.txt", during the lessons:
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 following example opens the "webdictionary.txt" file in a read-only mode.
It also generates a message if the
fopen() function is unable to open the file:
Example
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Error: Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>
Run example »
Tip: The
fread() and the
fclose() functions will be
explained below.
PHP fread() - Read a File
The fread() function reads from an open file.
The first parameter of
fread() contains the name of the file to read from and
the second parameter is optional and specifies the maximum number of bytes to read.
The following PHP code reads the "webdictionary.txt" file to the end:
fread($myfile,filesize("webdictionary.txt"));
PHP fclose() - Close a File
The fclose() function is used to close an open file.
It's a good programming practice to close all files after you have finished with them. You don't want an open file running around on your server taking up resources!
The fclose() requires the name of the file (or a variable that holds the
filename) we want to close:
<?php
$myfile = fopen("webdictionary.txt", "r");
// some code to be executed....
fclose($myfile);
?>
PHP fgets() - Read a Single Line
The fgets() function is used to read a single line from a file.
The example below 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);
?>
Run example »
Note: After a call to the
fgets() function, the file pointer has moved to the next line.
PHP feof() - Check End-Of-File
The feof() function checks if the "end-of-file" (EOF) has been reached.
The feof() function is useful for looping through data of unknown length.
The example below reads the "webdictionary.txt" file line by line, until end-of-file is reached:
Example
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
echo fgets($myfile) . "<br>";
}
fclose($myfile);
?>
Run example »
PHP fgetc() - Read a Single Character
The fgetc() function is used to read a single character from a file.
The example below reads the "webdictionary.txt" file character by character, until end-of-file is reached:
Example
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one character until end-of-file
while(!feof($myfile)) {
echo fgetc($myfile);
}
fclose($myfile);
?>
Run example »
Note: After a call to the
fgetc() function, the file pointer moves to the next character.
Complete PHP Filesystem Reference
For a complete reference of filesystem functions, go to our complete PHP Filesystem Reference.