PHP File Handling

This PHP File Handling tutorial helps you learn how to handle files while working on web applications. 

 

What is PHP File Handling?

When a web application is developed using PHP, we often need to work with external files, such as writing user data into a file or reading data from a file, etc. So, you must know how to handle files while working on any web application.

 

File Handling Operations

It starts with creating a file, next reading its content, then writing into a file till appending data into an existing file, and lastly, closing the file. Pre-defined functions are provided by PHP for all these operations. So let's begin by learning about these functions.

  • Create a File: fopen()
  • Open a File: fopen()
  • Read a File: fread()
  • Write to a File: fwrite()
  • Append to a File: fwrite()
  • Close a File: fclose()
  • Delete a File: unlink()

Did you notice that the same functions have been specified for multiple file operations? It is because by changing one or more arguments, you can use the same function to perform multiple operations on the file.

 

Uses of File in Applications

Some practical use cases where you need files in your web application are as follows:

  • Data is often stored in JSON files, and Php code reads the file and then displays the data on the web page.
  • Whereas database is not used in some simple applications. In fact, data is stored in files.
  • Some web applications require you to prepare a file for the user to download, then a file is created by PHP code, write data in it, and then allow the user to download it.

 

Create a File with PHP

When the function fopen() is used to open a file that doesn't exist, that file gets created.

Example:

<?php

$myfile = 'file.txt';
//opens the file.txt file or implicitly creates the file
$handle = fopen($myfile, 'w') or die('Cannot open file: '.$myfile); 

?>

The fopen() function will take the filename as the first argument, and the second argument will represent the mode in which the file will be opened.

However, in case a file is not present, and the fopen() is used to create a file, then keep the mode as w, which implies write mode. You will learn about the different modes in the below section.

 

Open a File with PHP

The same fopen() function is used for opening a file. You can open a file for many reasons, such as reading the file's content, to write new content to a file, or for appending additional content to the already existing content in the file.

Let's consider an example, and then we will talk about the different modes in which a file can be opened.

<?php

$myfile = 'file.txt';
//opens the file.txt file
$handle = fopen($myfile, 'w') or die('Cannot open file: '.$myfile); 

?>

 

fopen() function will take two mandatory arguments, the filename, and the mode.

Given below are the different modes along with the literal, which should be passed as an argument in the fopen() function.

ModeString LiteralDescription
Write modew

In case the file exists, then the file is opened to allow write operation

 in the file, and if the file doesn't exist, then a new file will be created. 

In the write mode, all the existing data in the file will get erased.

Read moder

In the read mode; the file is opened with the file pointer 

starting from the file's beginning.

Append modea

The file is opened in write mode where existing content 

of the file is not erased, and the new content is added after 

the existing content.

Create Write-only filex

It creates a new file with write-only access. 

If the file is already existing, then an error is returned.

 

Other than the mode specified above, you can add + along with the string literals for allowing both read and write (default) operations for a given mode.

For instance, the r+ mode opens the file for reading and writing both. Likewise, the w+, a+ enable read operation on the file along with the default write and append operations, respectively.

Technically, the file is not opened; the resource (file) is bound to a stream by fopen(); you can then use it to read from the file or write data to the file.

Also, in case the file is a local file, then the filename should be a fully qualified name along with the relative path. The filename can also be a URL for specifying a remote file's path. If PHP realises that the file path is not local, the value of the allow_url_fopen property in the php.ini(PHP's configuration file) will be checked by it. In case it is false, a warning will be printed by PHP, and the fopen() call will fail.

 

Close a File with PHP

A file resource should be closed after using it. You can use the PHP fclose() function to close a file resource. This function will either take the file name as an argument or the variable holding the file resource pointer as an argument.

<?php

$myfile = 'file.txt';
//opens the file.txt file or implicitly creates the file
$handle = fopen($myfile, 'w') or die('Cannot open file: '.$myfile); 

// closing a file
fclose($handle);

?>