PHP File Handling

Using PHP file handling mechanism, we can get external file resources to store as a reference. We have slightly touch about file resources, while discussing about PHP Resource Data.

PHP provides set of in-built functions to handle files. Some of the functions are, fopen(), file_exists(), file_get_contents() and etc.

Some of the basic operations to deal with files are listed below.

  • Opening file
  • Working with file read, write and append
  • Closing file

Opening file

To open a file, we need to specify the name of that file and mode. The mode of operation deals with the access limitation of the file to be opened.

Mode of Operation

In PHP file handling, there are four set of possible modes. These are,

  • {r and r+} – To read existing file.
  • {w and w+} – To change the entire file content.
  • {a and a+} – To add content after existing file content.
  • {x and x+} – To create new file and work with it.

php_file_handling

Read existing file.

In PHP, the following types of two file handing modes are used to read existing file. The file pointer will points to the start of file.

Mode of Operation File Mode File Pointer Position
r read-only Start of the file content
r+ read-write Start of the file content

Using fopen() function, we can open a file by it’s name and corresponding mode. For example,

$filePointer = fopen("hello.txt","r");
while(!feof($filePointer)){
echo fgets($filePointer). "<br>";
}

Note: We should ensure that the specified file is exist. If not, the following error will occur,

Warning: fopen(hello.txt) [function.fopen]: failed to open stream: No such file or directory in ...

Warning: fgets() expects parameter 1 to be resource, boolean given in ...

Write into a file.

To perform the write operation, we have two choices to select the mode of the file to be opened. These are,

Mode of Operation File Mode File Pointer Position
w write-only Start of the file content
w+ read-write Start of the file content

By using these modes, the entire file content will be cleared and the pointer will focus the start position of the file content. This method is used to change the existing file content, completely.

For example,

$filePointer = fopen("hello.txt","w");
fwrite( $filePointer, "PHP POT: MySQL Query\n" );

Append content to the file

In this operation, the existing content of the file will not be cleared. Rather, we can add new content continuously. Here, file pointer will point end of file. And the possible mode of operations are,

Mode of Operation File Mode File Pointer Position
a write-only Start of the file content
a+ read-write Start of the file content

The code will be similar to that of file-write except the mode specified to open file. For example,

$filePointer = fopen("hello.txt","a");

Note: In PHP, write and append mode will open the specified file if exists. Otherwise, new file will be created for performing file write and append.

create new file

Here also two file handing modes available that are used to write into a newly created file.

Mode of Operation File Mode File Pointer Position
x write-only. -
x+ read-write. -

For example,

$filePointer = fopen("hello.txt","x");
fwrite( $filePointer, "PHP POT: FUNCTIONS\n" );

Caution:- If the file is already exist, then the execution will be stopped with following error.

Warning: fopen(hello.txt) [function.fopen]: failed to open stream: File exists in ... on line ...

Warning: fwrite() expects parameter 1 to be resource, boolean given in ... on line ...

Closing File

After performing all the above file operations of PHP, we need to close it by using fclose() function. fclose() function should hold the file pointer which is the reference for the file resources. In PHP, we can use fclose() as shown below.

fclose( $filePointer);

This PHP tutorial was added on May 13, 2013.

«

»