Home » PHP » PHP File Handling
This PHP File Handling tutorial helps you learn how to handle files while working on web applications.
When a web application is developed using PHP, we often need to work with external files, such as writing user data into a file or reading data from a file, etc. So, you must know how to handle files while working on any web application.
It starts with creating a file, next reading its content, then writing into a file till appending data into an existing file, and lastly, closing the file. Pre-defined functions are provided by PHP for all these operations. So let's begin by learning about these functions.
Did you notice that the same functions have been specified for multiple file operations? It is because by changing one or more arguments, you can use the same function to perform multiple operations on the file.
Some practical use cases where you need files in your web application are as follows:
When the function fopen() is used to open a file that doesn't exist, that file gets created.
Example:
<?php
$myfile = 'file.txt';
//opens the file.txt file or implicitly creates the file
$handle = fopen($myfile, 'w') or die('Cannot open file: '.$myfile);
?>
The fopen() function will take the filename as the first argument, and the second argument will represent the mode in which the file will be opened.
However, in case a file is not present, and the fopen() is used to create a file, then keep the mode as w, which implies write mode. You will learn about the different modes in the below section.
The same fopen() function is used for opening a file. You can open a file for many reasons, such as reading the file's content, to write new content to a file, or for appending additional content to the already existing content in the file.
Let's consider an example, and then we will talk about the different modes in which a file can be opened.
<?php
$myfile = 'file.txt';
//opens the file.txt file
$handle = fopen($myfile, 'w') or die('Cannot open file: '.$myfile);
?>
fopen() function will take two mandatory arguments, the filename, and the mode.
Given below are the different modes along with the literal, which should be passed as an argument in the fopen() function.
Mode | String Literal | Description |
---|---|---|
Write mode | w | In case the file exists, then the file is opened to allow write operation in the file, and if the file doesn't exist, then a new file will be created. In the write mode, all the existing data in the file will get erased. |
Read mode | r | In the read mode; the file is opened with the file pointer starting from the file's beginning. |
Append mode | a | The file is opened in write mode where existing content of the file is not erased, and the new content is added after the existing content. |
Create Write-only file | x | It creates a new file with write-only access. If the file is already existing, then an error is returned. |
Other than the mode specified above, you can add + along with the string literals for allowing both read and write (default) operations for a given mode.
For instance, the r+ mode opens the file for reading and writing both. Likewise, the w+, a+ enable read operation on the file along with the default write and append operations, respectively.
Technically, the file is not opened; the resource (file) is bound to a stream by fopen(); you can then use it to read from the file or write data to the file.
Also, in case the file is a local file, then the filename should be a fully qualified name along with the relative path. The filename can also be a URL for specifying a remote file's path. If PHP realises that the file path is not local, the value of the allow_url_fopen property in the php.ini(PHP's configuration file) will be checked by it. In case it is false, a warning will be printed by PHP, and the fopen() call will fail.
A file resource should be closed after using it. You can use the PHP fclose() function to close a file resource. This function will either take the file name as an argument or the variable holding the file resource pointer as an argument.
<?php
$myfile = 'file.txt';
//opens the file.txt file or implicitly creates the file
$handle = fopen($myfile, 'w') or die('Cannot open file: '.$myfile);
// closing a file
fclose($handle);
?>