Introduction

Files are an important part of development. They are used on the client-side as well as server-side. A developer must understand, at least, the basic concepts of file handling. 

PHP supports file handling. Being a server-side programming language, files and directories can be managed on a web server using PHP. In this tutorial, we will discuss how to manage files using PHP.

Files in PHP

In PHP, we can perform several different operations on files. For example, we can open a file and perform operations such as reading or writing, or both. Let’s start by discussing how to open a file in different modes.

Opening and closing files

The first step is to open a file. PHP provides the fopen() function to open an existing file. The fopen() function has two parameters:

FilenameSet the name of the file. The parameter accepts local files, but also an URL (if allow_url_fopen is set to true).
ModeSpecifies the type of access that will be granted (f.e. read access, or read and write access).

A simple example: 


<?php
$file = fopen("demo.txt", "r");
?>


In the above code, “demo.txt” is the filename while “r” is the mode.

PHP has several defined modes. Following is the list of all the modes available in PHP.

  • r – To open the file for reading.
  • r+ – To open the file for reading as well as writing.
  • w – To open the file for writing. It clears the content of the file and tries to create a new file if it does not exist.
  • w+ – To open the file reading as well as writing. It clears the content of the file and tries to create a new file if it does not exist.
  • a – To append content on an existing file. It does not clear the content of the file and tries to create a new file if it does not exist.
  • a+ – To open the file for reading and appending. It does not clear the content of the file and tries to create a new file if it does not exist.
  • x – To open the file for reading but returns false if the file does not exist.
  • x+ – To open the file for reading as well as writing. It also returns false if the file does not exist. 
  • c – To open the file for reading. It clears the content and tries to create a new file if it does not exist. 
  • c++ – To open the file for reading and writing. It clears the content and tries to create a new file if it does not exist. 

Though PHP automatically closes all the files after the operations are performed, it is recommended to close the file manually. PHP provides the fclose() function to close the opened file. 

The fclose() function has one parameter – filename. Observe the following code.



<?php
$file = fopen("demo.txt", "r");
fclose($file);
?>

PHP functions for files

fopen() and fclose() are primary functions used for opening and closing files in PHP, respectively. But PHP provides additional functions to help the programmers. Let’s discuss some of these in-built PHP functions.

file_exists()

Working with files is prone to errors and warnings. A situation can arise where the specified file does not exist. This can lead to errors or warnings. To avoid such errors and warnings, it is recommended to use the file_exists() function before using fopen() to check whether the file exists or not.

Seee the following code. 


<?php

// check if the file exists

if (file_exists("demo.txt"))
{
  // open the file in read mode
  $file = fopen("demo.txt","r");
  // close the file
  fclose($file);
} else {
    echo "file does not exist";
}

?>

In the above code, the file_exists() function is used with the if-statement to check if “demo.txt” exists or not. 

fread()

A file can be huge. We may need to read the entire content or just a part of it. Suppose we need to read only the first 10 characters of the entire content of the file. In such cases, we can use the fread() function. 

The fread() function has two parameters – file and length in bytes.

Observe the following code.


<?php

// check if the file exists

if (file_exists("demo.txt"))
{
  // open the file in read mode
  $file = fopen("demo.txt","r");
  // use fread() function
  $data = fread($file, "10");
  echo $data

  // close the file
  fclose($file);
} else {
    echo "file does not exist";
}
?>

In the above code, the fread() function is used to read 10 bytes from the “demo.txt” file. 

fwrite()

The fwrite() function is used to write the data on a file. It takes two parameters – file and string.

Observe the following code.


<?php

//checking the existence if file

if(file_exists(“demo.txt”))

{

    //opening file with “w” mode

    $file = fopen(“demo.txt”, “w”);

    fwrite($file, “This is a string”);

}

else{

    echo “file does not exist”;

}

?>


There are two scenarios in the above code. 

  • If “demo.txt” does not exist, the fwrite() function will create a new file and write “This is a string” on it. 
  • If “demo.txt” exists, the fwrite() function will overwrite the content of the file but if you want to append the content, then just use “a” mode instead of “w”.

filetype()

To check the type of the file, PHP provides the filetype() function. Following are the possible values that filetype() function can return.

  • char
  • fifo
  • dir
  • file
  • link
  • socket
  • block
  • unknown

unlink()

The unlink() function is used to delete a file or directory. It returns true if the file or directory is deleted and false if not. 

Observe the following code. 


<?php

//checking the existence if file

if(file_exists(“demo.txt”))

{

    //using unlink() function to delete demo.txt

    if(unlink(“demo.text”)){

    echo “file deleted”;

    }

    else{

    echo “error while deleting file”;

    }

}

else{

    echo “file does not exist”;

}

?>


In the above code, the unlink() function is used to delete “demo.txt”.

Summary

  • The fopen() function is used to open a file. 
  • There are different modes to open a file using the fopen() function.
  • The fclose() function is used to close a file.
  • PHP provides several inbuilt functions to work with files.
  • Some of the commonly used inbuilt functions are file_exists(), fread(), fwrite(), filetype(), and unlink().