- PHP File Open/Read/Close
- PHP Open File — fopen()
- Example
- PHP Read File — fread()
- PHP Close File — fclose()
- PHP Read Single Line — fgets()
- Example
- PHP Check End-Of-File — feof()
- Example
- PHP Read Single Character — fgetc()
- Example
- Complete PHP Filesystem Reference
- fclose
- Return Values
- Examples
- See Also
- User Contributed Notes 7 notes
- PHP File Open/Read/Close
- PHP Open File — fopen()
- Example
- PHP Read File — fread()
- PHP Close File — fclose()
- PHP Read Single Line — fgets()
- Example
- PHP Check End-Of-File — feof()
- Example
- PHP Read Single Character — fgetc()
- Example
- Complete PHP Filesystem Reference
PHP File Open/Read/Close
In this chapter we will teach you how to open, read, and close a file on the server.
PHP Open File — fopen()
A better method to open files is with the fopen() function. This function gives you more options than the readfile() function.
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 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. The following example also generates a message if the fopen() function is unable to open the specified file:
Example
$myfile = fopen(«webdictionary.txt», «r») or die(«Unable to open file!»);
echo fread($myfile,filesize(«webdictionary.txt»));
fclose($myfile);
?>?php
Tip: The fread() and the fclose() functions will be explained below.
The file may be opened in one of the following modes:
Modes | Description |
---|---|
r | Open a file for read only. File pointer starts at the beginning of the file |
w | Open a file for 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 |
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 |
r+ | Open a file for read/write. File pointer starts at the beginning of the file |
w+ | Open a file for 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+ | Open a file for 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+ | Creates a new file for read/write. Returns FALSE and an error if file already exists |
PHP Read File — fread()
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 specifies the maximum number of bytes to read.
The following PHP code reads the «webdictionary.txt» file to the end:
PHP Close File — fclose()
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 Read Single Line — fgets()
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
$myfile = fopen(«webdictionary.txt», «r») or die(«Unable to open file!»);
echo fgets($myfile);
fclose($myfile);
?>?php
Note: After a call to the fgets() function, the file pointer has moved to the next line.
PHP Check End-Of-File — feof()
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
$myfile = fopen(«webdictionary.txt», «r») or die(«Unable to open file!»);
// Output one line until end-of-file
while(!feof($myfile)) echo fgets($myfile) . «
«;
>
fclose($myfile);
?>?php
PHP Read Single Character — fgetc()
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
$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);
?>?php
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.
fclose
The file pointer must be valid, and must point to a file successfully opened by fopen() or fsockopen() .
Return Values
Returns true on success or false on failure.
Examples
Example #1 A simple fclose() example
$handle = fopen ( ‘somefile.txt’ , ‘r’ );
See Also
User Contributed Notes 7 notes
It is a GOOD_THING to check the return value from fclose(), as some operating systems only flush file output on close, and can, therefore, return an error from fclose(). You can catch severe data-eating errors by doing this.
I learned this the hard way.
Generally, it’s always a good idea to close a file when you’re done with it. It’s very easy for something to go wrong and corrupt a file that hasn’t been closed properly. If you’re concerned about efficiency, the overhead is negligible.
In case you have some trouble to properly disconnect some client streams opened with stream_socket_server / stream_select you should give a try to stream_socket_shutdown.
Note that from PHP 8.0 onwards, attempting to close a stream that is already closed will throw a fatal TypeError.
Prior to PHP 8, this just caused a warning (that you can silence with @).
if you want to daysychain a filehandle through some functions and each function is allowed to close th file you might look in a following function first, if the handle is still valid.
Opening a file, there often will be used a code like
if (!$fh = fopen($filename, $mode)) return false;
But if you possably have closed the file and you want to check that, a smililar statement would not work.
DOES NOT WORK: if (!$fh) end_of_chain();
use beter: if (is_resource($fh)) end_of_chain();
It is very important to make sure you clear any incoming packets out of the incoming buffer using fread() or some equivalent. Although you can call fclose() the socket does not actually shut down until the inbound packets have been cleared. This can lead to some confusion.
In response to kumar mcmillan ‘gotcha’ note below, we get a different result on a W2K machine:
$file_pointer = fopen ( ‘textfile.dat’ , ‘r’ );
fclose ( $file_pointer );
echo ‘$file_pointer is resource = ‘ . ( is_resource ( $file_pointer ) ? ‘true’ : ‘false’ );
?>
output:
$file_pointer is resource = false
PHP File Open/Read/Close
In this chapter we will teach you how to open, read, and close a file on the server.
PHP Open File — fopen()
A better method to open files is with the fopen() function. This function gives you more options than the readfile() function.
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 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. The following example also generates a message if the fopen() function is unable to open the specified file:
Example
$myfile = fopen(«webdictionary.txt», «r») or die(«Unable to open file!»);
echo fread($myfile,filesize(«webdictionary.txt»));
fclose($myfile);
?>?php
Tip: The fread() and the fclose() functions will be explained below.
The file may be opened in one of the following modes:
Modes | Description |
---|---|
r | Open a file for read only. File pointer starts at the beginning of the file |
w | Open a file for 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 |
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 |
r+ | Open a file for read/write. File pointer starts at the beginning of the file |
w+ | Open a file for 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+ | Open a file for 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+ | Creates a new file for read/write. Returns FALSE and an error if file already exists |
PHP Read File — fread()
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 specifies the maximum number of bytes to read.
The following PHP code reads the «webdictionary.txt» file to the end:
PHP Close File — fclose()
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 Read Single Line — fgets()
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
$myfile = fopen(«webdictionary.txt», «r») or die(«Unable to open file!»);
echo fgets($myfile);
fclose($myfile);
?>?php
Note: After a call to the fgets() function, the file pointer has moved to the next line.
PHP Check End-Of-File — feof()
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
$myfile = fopen(«webdictionary.txt», «r») or die(«Unable to open file!»);
// Output one line until end-of-file
while(!feof($myfile)) echo fgets($myfile) . «
«;
>
fclose($myfile);
?>?php
PHP Read Single Character — fgetc()
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
$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);
?>?php
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.