October 29, 2026

PHP – Working with Files

In this tutorial, we would see how to manipulate files and directories. The following would be covered:

  1. Opening and Closing Files
  2. Reading File Content
  3. Writing to a File

 

1. Opening and Closing Files

You can open a file in PHP using the fopen() function. This function takes two arguments: the file path and the file mode in which it would open. There are six different file modes as specified in the table below:

SN Mode & Description
1 r  – Opens the file for read-only and place the file pointer at the beginning of the file.
2 r+ – Opens the file for read/write and places the file pointer at the beginning of the file.
3 w – Opens the file for write only and places the file pointer at the beginning of the file.

the file is truncated to zero length.

If files does not exist then a new file is created.

4 w+ – Opens the file for reading and write-only and places the file pointer at the beginning of the file.

the file is truncated to zero length.

If files does not exist then a new file is created.

5 a  – Opens the file for reading and write-only and places the file pointer at the end of the file.

the file is truncated to zero length.

If files does not exist then a new file is created.

6 a+

Opens the file for reading and appending only and the pointer is placed at the end of the file. The file content is preserved

the file is not truncated to zero length.

If files does not exist then a new file is created.

The fopen() function will return a file pointer for reading the file if the operation is successful. However, if the operation fails, the function returns false. After reading or writing a file, you have to close the file using the fclose() function. This function returns true if the close operation succeeds, otherwise it returns false. We’ll an example in the next section.

 

2. Reading File Content

Once a file is successfully opened, the content can be read using the fread() function. This function take two arguments: the first argument is the pointer (returned by fopen) while the second argument is the length of the file in bytes.

The length of the file can be obtained using the filesize() function. This function takes two arguments as well: the file name and the file size in bytes.

The steps for reading a file can be summarised as follows:

  • open the file using the fopen() funtion
  • get the length of the file using the filesize() function
  • read the content of the file using the fread() function
  • close the file using fclose() function

Let’s take an example that reads the content of a file and displays the content to a web page

<?php 
    $filePointer = fopen("names.txt", "r+");

    if ($fileSize == false){
        echo "Could not open file";
        exit();
    }
    
    $fileSize = filesize("names.txt");
    $fileContent = fread($filePointer, $fileSize);
    fclose($filePointer);

    echo "File size is: $fileSize bytes";
    echo "<pre> $fileContent </pre>";
?>

 

3. Writing to a File

As mentioned before, you can write some text into a new file or an existing file. You can do this using the fwrite() function. This function takes two arguments: first is the file pointer and the second is the string of data to be written. There is also a third optional argument, that is the size of data to write. If this argument is specified, then the write will stop once the given size of data is written.

The example below creates a new file and writes some text inside. You can use the function file_exists() to check if a file exists. This function takes the name of the file as an argument.

<?php 
    $filename = "names.txt";
    $filePointer = fopen($filePointer, "w");
    
    if($filePointer == false){
        echo "Could not open file";
        exit();
    }

    $textToWrite = "This is an example of writing to file";

    fwrite($filePointer, $textToWrite);
    fclose($filePointer);
?>