<< PHP File Tutorial | PHP Tutorials | PHP Read File >>

PHP Open File - fopen()

The function that is used most of the time to open files in PHP is the fopen() function. To be able to use this function you need to know the name of the file you'll be working with and the mode. The file name can be a relative path (/file.txt, file.txt) or a URL if your PHP configuration supports it (most of the time not though). There will be working examples down below, but to get a feel for the function it will look something like this:

<?php
$file_handle = fopen('example.txt', 'w') or die('Error opening file');
fclose($file_handle);
?>

Different File Modes

There are countless reasons why you might want to open a file in PHP. The only thing is, how do you tell the PHP parser what you want to do? This can be done using modes. The following modes can be used when using the fopen() function:

  • r

    This will be used if you want to read (and only read) from the file. The pointer will be placed at the beginning of the file.
  • w

    This will be used to write into a file. The only thing is, it will overwrite all the information currently in the file with the information you want to input (pointer at the beginning of file). Also, if the file doesn't exist the PHP engine will attempt to create it for you.
  • a

    This will open the file to be used for writing. The only difference from the w mode is that it will not overwrite all the data in the file (pointer at the end of the file). Therefore it will 'append' to the file.
  • x

    This will create and open a file for writing only. The only problem is, it will give an error if the file already exists. It is specifically designed to create files for you.

There are also some more advanced modes that can be used:

  • r+

    This is used to read and write to a file. If you write to the file, it will overwrite all the information currently in the file (pointer at start of file).
  • w+

    This is almost exactly the same as r+. The only difference is, if the file doesn't exist it will attempt to create it for you.
  • a+

    This will open the file for reading and writing. If you write to the file it won't overwrite any of the data, and will just 'append' to it (pointer at end of file).
  • x+

    This is essentially the same as x except it also can read from the file, which is useless, because you already have the data you want to enter.

*Note: A file pointer is just the location in the file where the PHP parser is.

Opening the File

Opening a file is the first thing that needs to be done before you can do anything else with it. The fopen() function should be used like this:

$file_handle = fopen('file_name', 'open_mode') or die('Error opening file');

The file_name and open_mode need to be replaced with what you need. Here is another example of just opening and closing a file:

<?php
$file_handle = fopen('example.txt', 'w') or die('Error opening file.');
fclose($file_handle);

That will simply open example.txt, or create it if it doesn't exist. It then will close the handle to the file.

Creating a File

Using the fopen() function you are able to create a file. This file can then be dealt with like a regular file, had you had uploaded it. Here is a simple example of how to create example.txt:

<?php
$data = 'This is the new file content.';
$file_handle = fopen('example.txt', 'x') or die('Error creating file.');
fwrite($file_handle, $data);
fclose($file_handle);
?>

Here you see three functions; fopen(), fwrite() and fclose().

The fwrite() function requires two parameters. The first one is the file handle to which we are writing. In this case it is stored in the $file_handle variable. The second parameter is the data which is going to be written, in this case we had it previously stored in the $data variable. Depending on which mode you chose, it could overwrite all the data or just append to the end of the file.

The fclose() is necessary after you are done dealing with the file. The only thing it does is closes the connection to the file.

You can test this code on your website! Once you run it you should be able to see the example.txt file in your FTP program :)

<< PHP File Tutorial | PHP Tutorials | PHP Read File >>