File handling in PHP

F
Trusted SSL from just $3.44
Time and date functions in PHP
Strings in PHP

Being able to handle files is an important aspect of any programming language, including PHP.
As a PHP developer it is a must to understand the basics of opening, reading and closing files. And not only files, but also the opening, reading and closing of directories.
Thankfully this task is easy to accomplish in PHP.

Besides discussing the above this article also answers the most common questions about files in PHP.

PHP file handling read and write
Upload a file using PHP
File Handling using PHP
Private file handling with laravel
Change the maximum upload file size
How to get a file’s extension in PHP?
How can I find the php.ini file used by the command line?
Download File to the server from URL
Print array to a file
Create a CSV File for a user in PHP

File handling 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).
<?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);
?>

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.

<?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()

The size of a file can be quite big. Because of this we might choose not to read the full content of a file, but only a part of it. For cases like these we have the the fread() function. It allows us for example to only read ten characters. 

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

<?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 data in a file. It requires two parameters – file and string.

<?php
//checking the existence of the file

if(file_exists("demo.txt"))
{
  //opening the file in “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 will output “This is a string”. 
  • If “demo.txt” exists, the fwrite() function will overwrite the content of the file.
    Note: In case you want to append (add) to the content you can use the “a”-mode instead of the “w”-mode.

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. 



<?php
//check if the file exists
if(file_exists("demo.txt")) {
  //use unlink() function to delete demo.txt
  if(unlink("demo.text")){
    echo "file deleted";
  }
  else {
    echo "error while trying to delete the file";
  }
  else {
    echo "the file doesn't exist";
  }
}
?>

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

Summary

Time and date functions in PHP
Strings in PHP

Tutor Network

Share this

Learn PHP from A to Z