Opening & Closing Files

The ability to manipulate files can be both fun and useful. The first step of the process would be to identify the file that you want to manipulate, and open it.

PHP uses the fopen() function for the purpose of opening an existing file or creating a new file if the specified file does not already exist. The function syntax is: fopen(filename, method);

It is probably obvious that "filename" refers to the name (and also the path) of the file, but method may not be quite as obvious. It refers to the type of access that is allowed each time the file is opened. The most common options are:

Mode Description
r Open for reading only; place the file pointer at the beginning of the file.
r+ Open for reading and writing; place the file pointer at the beginning of the file.
w Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
w+ Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
a Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
a+ Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
x Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it.
x+ Create and open for reading and writing; otherwise it has the same behavior as 'x'.
c Open the file for writing only. If the file does not exist, it is created. If it exists, it is neither truncated (as opposed to 'w'), nor the call to this function fails (as is the case with 'x'). The file pointer is positioned on the beginning of the file.
c+ Open the file for reading and writing; otherwise it has the same behavior as 'c'.

When using fopen(), a file handle is returned which identifies the open file connection and will later be used to read/write/close the file, all very important steps. We can store that file handle in a variable. Let's look at some examples of how to open a file.

<?php
  $FileHandle1 = fopen('storage.txt', 'w');

  $FileHandle2 = fopen('http://www.your-website.com/', 'a+') or die("Uh-oh! Errors!");

  $FileName = "directory-path-to-file/filename.php";

  $FileHandle3 = fopen($FileName, 'r');
?>

So as you can see, you can mix and match your options as needed to get the job done.

The most important part of opening a file is remembering to close it when you are done editing. The fclose() function accepts one parameter, that parameter being the file handle of the open file that needs closed.

<?php
  $FileHandle1 = fopen('storage.txt', 'w');
  fclose($FileHandle1);

  $FileHandle2 = fopen('http://www.your-website.com/', 'a+') or die("Uh-oh! Errors!");
  fclose($FileHandle2);

  $FileName = "/directory-path-to-file/filename.php";
  $FileHandle3 = fopen($FileName, 'r');
  fclose($FileHandle3);
?>

If you are getting errors when trying to open/edit/close files, it might be because PHP does not have permission to edit files on the server. You will need write permission enabled in the directory that the file is stored in.

Next up, we will learn how to read and write to files.

Summary:

Function Description
fopen() Opens a File Or a URL
fclose() Closes An Open File Pointer