How to Open Read Close a File in PHP

If you are working with web development or programming, you know that file handling is an essential part of developing web applications. PHP provides built-in functions for handling files, making it easy to open, read, and close files using just a few lines of code.

In this article, we will explore the basics of file handling in PHP, with a focus on opening, reading, and closing files.

How to Open a File in PHP

Opening the file can be done for several reasons, such as reading its contents, adding new data, or appending new data to the file’s existing content. In PHP, a file is opened using the fopen() function.

There are different modes to open the file in PHP which are given as:

  • w: The file is opened in write-only mode. If the file doesn’t exist, first it is created. Otherwise, the contents are modified.
  • r: This mode is used for opening the file in read-only mode.
  • a: This mode is utilized for opening the file in append mode. That means the content are added at the end of existing content.
  • w+: This is used to open a file in write and read If a file is missing, first it is created. Otherwise, the contents of the file are modified.
  • r+: It is utilized for opening the file in write as well as read
  • a+: It is utilized to open the file in read and write mode to add the contents at the end of a

The fopen() function has the syntax given as:

fopen(string $filename, string $mode);

This function takes two arguments:

  • filename: This is the name of the file which is used to open.
  • mode: This can be any mode according to the operation performed.

How to Read a File in PHP

You can read contents from a file using the fread() function. We must specify the length of the file in bytes that we need to read.

The syntax of the fread() function is given as:

fread(resource $stream, int $length )

The fread() function takes two arguments:

$stream: This is a file system pointer resource, which is the result of the fopen() function.

$length: It specifies the maximum number of bytes to be read. When we need to read the entire file, pass the file size to the $length pointer.

The value returned by the fread() function is the file contents or it returns false when it fails to read.

How to Close a File in PHP

After using a file resource, it is the best practice to close it. We use fclose() function to close the file in PHP.

The syntax of the fclose() function is given as:

fclose(resource $stream);

It accepts either the file name or the variable containing the file resource pointer as a parameter.


Let’s look at the sample example that describes functions to open, read and close a file in PHP:

$file = fopen("file.txt", "r") or die("Unable to open file!");
echo "Contents read from file are: ";
echo fread($file,filesize("file.txt"));

The above program opens the text file named “file.txt” located in the current working directory using the fopen() function in read mode. Then it reads the file contents using fread() function and prints them on the output screen. The fclose() function is utilized to close a file at the end of the code.


In this tutorial, we covered the fundamentals of file handling in PHP. Throughout the article, we demonstrated the operation of multiple functions in PHP, such as fopen(), fread(), and fclose(), and use these functions in a code to open, read and close a file.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.