Opening a File with fopen() in PHP

If you want to work with a file from within the PHP script, then you first need to open the file. So when you open a file, you create a file handle. A file handle is a pointer associated with the open file that you can then use to access the file’s contents. When you have finished with the file, then you close it, which removes the file handle from memory.

File handles are resource data types. Here you will have a look at opening files with the fopen() function, and closing files with fclose().

Opening a File with fopen()

The fopen() function opens a file and returns a file handle associated with the file. The first argument passed to fopen() specifies the name of the file you want to open, and the second argument specifies the mode, or how the file is to be used.

Example

$handle = fopen( “./data.txt”, “r” );

The first argument can be just a filename ( “ string.txt “ ), in which case PHP will look for the file in the current directory, or it can be a relative ( ” ./string.txt “ ) or absolute (“/myfiles/string.txt “ ) path to a file. You can also specify a file on a remote Web or FTP server.

Example

$handle = fopen( “http://www.example.com/string.html”, “r” );
$handle = fopen( “ftp://ftp.example.com/pub/string.txt”, “r” );

A remote file can only be opened for reading, you cannot write to the file.
The second argument to fopen() tells PHP how you are going to use the file. It can take one of the following string values:

Value Description
r This value is used to Open the file for reading only. The file pointer is placed at the beginning of the file.
r+ This value is used to Open the file for reading and writing. The file pointer is placed at the beginning of the file.
w This value is used to Open the file for writing only. Any existing content will be lost. If the file does not exist, PHP attempts to create it.
a This value is used to Open the file for appending only. Data is written to the end of an existing file. If the file does not exist, PHP attempts to create it.
a+ This value is used to Open the file for reading and appending. Data is written to the end of an existing file. If the file does not exist, PHP attempts to create it.

The file pointer is PHP’s internal pointer that specifies the exact character position in a file where the next operation should be performed.
You can also append the value b to the argument to indicate that the opened file should be treated as a binary file (this is the default setting). Alternatively, you can append t to treat the file like a text file, in which case PHP attempts to translate end – of – line characters from or to the operating system’s standard when the file is read or written.

Example – To open a file in binary mode

$handle = fopen( “string.txt”, “rb” );

Although this flag is irrelevant for UNIX – like platforms such as Linux and Mac OS X, which treat text and binary files identically, you may find the text mode useful if you are dealing with files created on a Windows computer, which uses a carriage return followed by a line feed character to represent the end of a line (Linux and the Mac just use a line feed).

If you need your application’s data files to be readable by other applications on different platforms, you should use binary mode and write your code to use the appropriate end – of – line characters for the platform on which you are running. (The PHP constant PHP_EOL is handy for this; it stores the end – of – line character(s) applicable to the operating system that PHP is running on.)

By default, if you specify a filename that is not a relative or absolute path (such as “ string.txt “ ), PHP just looks in the current (script) directory for the file. However, you can optionally pass the value true as a third argument to fopen() , in which case PHP will also search the include path for the file.

If there was a problem in opening the file, fopen() returns false rather than a file handle resource.

Closing a File with fclose()

Here, you will learn how to close a file when you are finished with the working. When you have finished working with a file then it needs to be closed. To close the file you can use fclose() function by passing in the open file’s handle as a single argument, like

fclose( $handle );

PHP should close all open files automatically when the script terminates, it is good to close files from within the script as soon as you are finished with them because it frees them up quicker for use by other processes and scripts or even by other requests to the same script.