PHP Open File
Summary: in this tutorial, you will learn how to open a file in PHP using the fopen() function.
Introduction to the PHP fopen() function
Before reading from or writing to a file, you need to open it. To open a file, you use the fopen() function as follows:
fopen ( string $filename , string $mode , bool $use_include_path = false , resource $context = ? ) : resource
Code language: PHP (php)
The fopen() has the following parameters:
$filenameis the path to the file that you want to open.$modespecifies the type of access you require to the stream. See the table below.$use_include_pathdetermines whether thefopen()should also search for the file in the include path.$contextspecifies the stream context.
The fopen() returns the file pointer resource if it opens the file successfully or false if it fails to open the file.
The following table shows the type of access of the $mode parameter:
| Mode | Reading | Writing | File Pointer | File doesn’t exist, Create? | If the File Exists |
|---|---|---|---|---|---|
| ‘r’ | Yes | No | At the beginning of the file | No | |
| ‘r+’ | Yes | Yes | At the beginning of the file | No | |
| ‘w’ | No | Yes | At the beginning of the file | No | |
| ‘w+’ | Yes | Yes | At the beginning of the file | Yes | |
| ‘a’ | No | Yes | At the end of the file | Yes | |
| ‘a+’ | Yes | Yes | At the end of the file | Yes | |
| ‘x’ | No | Yes | At the beginning of the file | Yes | Return false |
| ‘x+’ | Yes | Yes | At the beginning of the file | Yes | Return false |
| ‘c’ | No | Yes | At the beginning of the file | Yes | |
| ‘c+’ | Yes | Yes | At the beginning of the file | Yes |
When working with a binary file, you need to append the characer b to the $mode argument. For example, the 'rb' mode opens a binary file for reading.
After opening a file and completing working with it, you should always close the file. If you don’t close a file properly, it may cause many issues. For example, you may not be able to access the file again.
To close a file, you pass the file pointer to the fclose() function:
fclose ( resource $stream ) : boolCode language: PHP (php)
The fclose() function returns true on success or false on failure.
It’s a good practice to check if a file exists before opening it.
PHP open files example
The following example uses the fopen() to open the readme.txt file for reading:
$filename = 'readme.txt';
if (!file_exists($filename)) {
die("The file $filename does not exist.");
}
$f = fopen($filename, 'r');
if ($f) {
// process the file
// ...
echo 'The file ' . $filename . ' is open';
fclose($f);
}
Code language: HTML, XML (xml)
How it works.
- First, use the
file_exists()function to check if thereadme.txtfile exists. If not, throw an exception saying that the file doesn’t exist. - Second, use the
fopen()function to open thereadme.txtfor reading. - Third, close the file in the
finallyblock only if the file pointer is notnull.
Summary
- Use the
fopen()function to open a file for reading, writing, and appending. - Always use the
fclose()function to close the file that was open by thefopen()function.