PHP Open File

Created with Sketch.

PHP Open File

Summary: in this tutorial, you will learn how to open a file in PHP using the fopen() function.

Introduction to the PHP fopen() function

Before reading from or writing to a file, you need to open it. To open a file, you use the fopen() function as follows:

fopen ( string $filename , string $mode , bool $use_include_path = false , resource $context = ? ) : resource

Code language: PHP (php)

The fopen() has the following parameters:

  • $filename is the path to the file that you want to open.
  • $mode specifies the type of access you require to the stream. See the table below.
  • $use_include_path determines whether the fopen() should also search for the file in the include path.
  • $context specifies the stream context.

The fopen() returns the file pointer resource if it opens the file successfully or false if it fails to open the file.

The following table shows the type of access of the $mode parameter:

ModeReadingWritingFile PointerFile doesn’t exist, Create?If the File Exists
‘r’YesNoAt the beginning of the fileNo
‘r+’YesYesAt the beginning of the fileNo
‘w’NoYesAt the beginning of the fileNo
‘w+’YesYesAt the beginning of the fileYes
‘a’NoYesAt the end of the fileYes
‘a+’YesYesAt the end of the fileYes
‘x’NoYesAt the beginning of the fileYesReturn false
‘x+’YesYesAt the beginning of the fileYesReturn false
‘c’NoYesAt the beginning of the fileYes
‘c+’YesYesAt the beginning of the fileYes

When working with a binary file, you need to append the characer b to the $mode argument. For example, the 'rb' mode opens a binary file for reading.

After opening a file and completing working with it, you should always close the file. If you don’t close a file properly, it may cause many issues. For example, you may not be able to access the file again.

To close a file, you pass the file pointer to the fclose() function:

fclose ( resource $stream ) : bool

Code language: PHP (php)

The fclose() function returns true on success or false on failure.

It’s a good practice to check if a file exists before opening it.

PHP open files example

The following example uses the fopen() to open the readme.txt file for reading:


$filename = 'readme.txt';

if (!file_exists($filename)) {
die("The file $filename does not exist.");

$f = fopen($filename, 'r');
if ($f) {
// process the file
// ...
echo 'The file ' . $filename . ' is open';

Code language: HTML, XML (xml)

How it works.

  • First, use the file_exists() function to check if the readme.txt file exists. If not, throw an exception saying that the file doesn’t exist.
  • Second, use the fopen() function to open the readme.txt for reading.
  • Third, close the file in the finally block only if the file pointer is not null.


  • Use the fopen() function to open a file for reading, writing, and appending.
  • Always use the fclose() function to close the file that was open by the fopen() function.

Leave a Reply

Your email address will not be published. Required fields are marked *