Working with files in PHP part 1 (file_get_contents, file_put_contents, file_exists, unlink, rename, copy, filesize)

Photo by Ilya Pavlov on Unsplash

Working with files in PHP

PHP Dec 25, 2022

The PHP programming language has a set of functions for working with files. Let's consider the simplest functions for reading, writing, deleting, moving, copying, renaming and checking the existence of files. In most cases, this set of actions will be enough for almost any project.

Reading a file

The file_get_contents function reads the contents of a file into a string. You can read both the contents of the file on the site with the script, and the contents on the remote server. Consider reading a locally located file:

<?php 
   echo file_get_contents('tiger.txt');
?>

When this code is executed, the contents of the 'tiger.txt' file, which is located in the same folder as the script with the example code, will be displayed on the screen. If you need to specify a file in a different location, then you must use the full path to the file on the system.

💡
If there are problems with the full path, then you can use the address of the site's root folder, which is in the $_SERVER['DOCUMENT_ROOT'] variable .

Then the path to the file can be written as:
$_SERVER['DOCUMENT_ROOT'] . '/tiger.txt'

Write file

The file_put_contents function can be used to write data to a file. Here is an example of its use:

<?php 
   $file = $_SERVER['DOCUMENT_ROOT'] . '/tiger.txt';
   $content = 'Lion, tiger on the roof';
   file_put_contents($file, $content);
?>

As a result of this example, the file 'tiger.txt', which is located in the root folder of the site, will be written with the string 'Lion, tiger on the roof'. If the file does not exist, it will be created. If the file already existed, its contents will be overwritten.

For the file_put_contents function, the following flags can be passed as the third parameter:

  • FILE_APPEND - causes the function to write the transferred data to the end of the file. Often used to create files with logs (history of any actions)
  • LOCK_EX Locks the file while it is being written so that no one can read or change it
  • FILE_USE_INCLUDE_PATH - Looks for a file in include directories. This flag is rarely used.

You can use multiple flags at once. To do this, you need to separate them with a vertical bar "|" when specifying them in the last parameter of the file_put_contents function. Let's demonstrate this:

<?php 
   $file = $_SERVER['DOCUMENT_ROOT'] . '/tiger.txt';
   $content = 'Lion, tiger on the roof';
   file_put_contents($file, $content, FILE_APPEND | LOCK_EX);
?>

As a result of executing this code, the line 'Lion, tiger is on the roof' will be added to the end of the 'tiger.txt' file. And if the file does not exist, it will be created.

Checking if a file exists

The file_exists function checks for the existence of a file. It takes a single parameter - the full path to the file. Often used inside an if( ) condition because it returns true if the file exists and false if the file does not exist. Because there is

<?php 
   $file = $_SERVER['DOCUMENT_ROOT'] . '/tiger.txt';
   if(file_exists($file)){
      echo 'File exists';
   }
?>

Deleting a file

PHP uses the unlink function to delete files. It, like all previous functions, is passed the absolute path to the file in the system. Let's give an example of deleting a file using it, but before deleting, we will check whether the file we are going to delete exists at all:

<?php 
   $file = $_SERVER['DOCUMENT_ROOT'] . '/tiger.txt';
   if(file_exists($file)){
      unlink($file);
      echo 'The file existed and was deleted';
   }
?>
💡
Due to the nature of UNIX systems, when a file is deleted with the unlink function, the file will exist as long as there is at least one symbolic or hard link to it.The unlink function returns true if the deletion is successful.

Renaming and moving a file

The rename function in PHP can rename a file or directory. It takes two parameters as input: the path to the file to be renamed, and the path to the file that will result from the renaming. You must specify the full path:

<?php 
   $fileOld = $_SERVER['DOCUMENT_ROOT'] . '/tiger.txt';
   $fileNew = $_SERVER['DOCUMENT_ROOT'] . '/mouse.txt';
   rename($fileOld, $fileNew);
?>

As a result of executing this function, the file 'tiger.txt' , which is located in the root folder of the site, will be renamed to '/mouse.txt' . If the file '/mouse.txt' already existed. then it will be overwritten.

As you might guess, this function can not only rename files and folders, but also move them to other directories. To do this, you need to specify a new path in the new name. For instance:

<?php 
   $fileOld = $_SERVER['DOCUMENT_ROOT'] . '/tiger.txt';
   $fileNew = $_SERVER['DOCUMENT_ROOT'] . '/home/tiger.txt';
   rename($fileOld, $fileNew);
?>

When executing this code, the 'tiger.txt' file will be moved to the '/home/' folder . The rename function returns true if the rename was successful.

File copy

Copying a file in PHP is done using the copy function. Which is similar to the rename function. It also takes two parameters. Here is an example of usage:

<?php 
   $fileOld = $_SERVER['DOCUMENT_ROOT'] . '/tiger1.txt';
   $fileNew = $_SERVER['DOCUMENT_ROOT'] . '/tiger2.txt';
   copy($fileOld, $fileNew);
?>

As a result of executing this function, the file 'tiger1.txt' , which is located in the root folder of the site, will be copied to '/tiger2.txt' . If '/tiger.txt' already existed, it will be overwritten. The copy function returns true if the copy is successful.

Determining the file size

Sometimes the task of determining the file size appears. This task is solved by the filesize function. This function has only one parameter - the full path to the file. And it returns the file size specified in bytes. Let's try to apply the function in practice:

<?php 
   $file = $_SERVER['DOCUMENT_ROOT'] . '/tiger.txt';
   echo 'The file size is ' . filesize($file) . ' byte';
?>

Tags

Anurag Deep

Logical by Mind, Creative by Heart