PHP Open File Tutorial

In this tutorial, we will look at how to create and open files in PHP. There are many ways of opening files in PHP, depending on whether you want to read from it, write to it, add data only to the end, or write data and forget the existing data which was in the file.

This tutorial shows the basics of file creation. Files are created using the fopen command which happens to be the same command used to open a file in PHP.

PHP Fopen Syntax - Create a file

The syntax for the fopen command is as follows:

$fileHandle = fopen($fileName, $mode);

The variable $filehandle is used to store the "address" of the file that we just opened and we need it so we can reference the same file later. The fopen command takes two parameters, the filename, and the mode. The filename is straightforward, and is just the path to the name of the file to be created. This path may be relative to the script which is calling the function or it may be an absolute path.

The mode is used to identify what we are planning to do with the file once we have opened it.

Before we continue, you need to know what a pointer is. A pointer tells PHP where in the file to start reading from or writing to.

Opening a file for Reading

When in this mode, the pointer is placed at the beginning of the file. The code below opens (for reading) a file called "test.txt" in the current directory. If the file does not exist, an error will be generated.

<?php
 
$fileName = "test.txt";
$fileHandle = fopen($fileName, 'r') or die("Error opening file!");
fclose($fileHandle);
 
?>

Opening a file for Writing

When in this mode, the pointer is placed at the beginning of the file and deletes the current contents of the file, if any. The code below opens (for writing) a file called "test.txt" in the current directory. If the file does not exist, it will be created. If it exists, the contents will be erased.

<?php
 
$fileName = "test.txt";
$fileHandle = fopen($fileName, 'w') or die("Error opening file!");
fclose($fileHandle);
 
?>

Opening a file for Appending

When in this mode, the pointer is placed at the end of the file and current contents of the file is not touched. The code below opens (for appending) a file called "test.txt" in the current directory. If the file does not exist, an error will be generated. After opening the file for appending, PHP will be ready to add additional data to the end of the file.

<?php
 
$fileName = "test.txt";
$fileHandle = fopen($fileName, 'a') or die("Error opening file!");
fclose($fileHandle);
 
?>

Those are the 3 main methods of opening a file. After we have a file open, the next step is to write to the file using PHP.

File Permissions and PHP

It is important to note that you need the relevant write permissions when creating files with PHP as you would with any other sort of file creation on a modern operating system. Please ensure that the webserver has write permissions in the directory that you want to create the file, or else the creation attempt will fail. For more information, see how to change file permissions in PHP.

As you saw above, it is fairly simple to create or open files in PHP. We hope this tutorial was helpful.

Link to this Page

Thank Tutorial Arena for This Tutorial.
Show your appreciation with a +1...