Create, Open and Close a File in PHP
To create a new file or to open an existing file, we use the fopen()
function provided by PHP and to close a file resource fclose()
function is used.
Create a File with PHP
When we use the function fopen()
to open a file which doesn't exist then that file gets created.
Let's take an example,
$myfile = 'file.txt';
//opens the file.txt file or implicitly creates the file
$handle = fopen($myfile, 'w') or die('Cannot open file: '.$myfile);
fopen()
function takes the filename as the first argument and the second argument represents the mode in which the file will be opened.
But, if a file is not present, and you are using fopen()
to create a file, then you should keep the mode as w
, which means write mode. We will learn about the various modes in the section below.
Open a File with PHP
To open a file, the same fopen()
function is used. There can be many reasons behind opening a file, like reading content of the file, writing new content to a file or appending additional content to the already existing content in the file.
Let's take a simple example, and then we will talk about the different modes in which a file can be opened.
$myfile = 'file.txt';
//opens the file.txt file
$handle = fopen($myfile, 'w') or die('Cannot open file: '.$myfile);
Again, fopen()
function takes two mandatory arguments, one is the filename and the second one represents the mode.
Following are the different modes along with the literal which should be passed as argument in the fopen()
function.
Mode | String Literal | Description |
Write mode | w | If the file exists then it is opened to allow write operation in it and if it doesn't exist, then a new file is created. In this mode all the existing data in file gets erased. |
Read mode | r | File is opened in read mode with the file pointer starting from the beginning of the file. |
Append mode | a | File is opened in write mode where existing content of the file is note erased. The new content in added after the existing content. |
Create Write-only file | x | A new file is created with write-only access. If the file already exists then error is returned. |
Apart from the mode specified above, we can add +
along with the string literals to allow both read and write(default) operation for a given mode.
For example, r+
mode opens file for read and write both. w+
, a+
allow read operation on the file along with the default write and append operations respectively.
Technically, file is not opened, fopen()
binds the resource(file) to a stream, which can then be used to read from the file or write data to the file.
Also, the filename should be fully qualified name along with relative path if the file is a local file. The filename can also be a URL to specify a remote fiel's path. In that case, once PHP realise that the file path is not local, it will check for the value of allow_url_fopen
property in the php.ini(PHP's configuration file). If it is false, PHP will print a warning and fopen()
call will fail.
Close a File with PHP
It is good practice to close a file resource after using it. In PHP fclose()
function is used to close a file resourcse. It takes the file name as argument or the variable holding the file resource pointer as argument. Let's take an example:
$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);
Time for an Example
Let's take a simple example where we will create a PHP file to create a text file and then open the same file to write content in it.
In the picture below we have described the directory structure which will help you to understand how to provide the path of the file and use it in the PHP file.
Below is the code we will keep in the read-file.php file to open file.txt text file to write data in it.
$myfile = 'include/file.txt';
//opens the file.txt file
$handle = fopen($myfile, 'w') or die('Cannot open file: '.$myfile);
// close a file
fclose($handle);
We can either provide complete path for the file to be read or we can provide relative path like in the example above.
In the upcoming tutorials we will learn how to read, write, append data to a file.