PHP fopen(): Open a file

The PHP fopen() function is used when we need to open a file or a URL. For example:

<?php
   $myfile = "codescracker.txt";
   if(fopen($myfile, "r"))
      echo "The file gets opened successfully, in 'r' mode.";
   else
      echo "Unable to open the file.";
?>

The output of the above PHP example is:

php fopen function

Since the file codescracker.txt is available in the current directory, it gets opened.

The fopen() function basically binds a named resource, specified by the name of a file, to a stream. For example, the above program can also be written as:

<?php
   $myfile = "codescracker.txt";
   $fs = fopen($myfile, "r");
   if($fs)
      echo "The file gets opened successfully, in 'r' mode.";
   else
      echo "Unable to open the file.";
?>

The $fs in the above example can be treated as a file handler, which is the variable used to handle the file opened by fopen(). Since the file is opened in reading mode, the only operation this file handler can perform on the file is the read operation.

PHP fopen() Function Syntax

The syntax of the fopen() function in PHP is:

fopen(fileName, fileOpeningMode, include_path, context)

The first two parameters are required, whereas the last two parameters are optional.

With the include_path parameter, you can include the path to look for the given file, where the context parameter is used to specify the context of the file handler.

Note: The fopen() function returns a file pointer or handler resource on success. Otherwise, it returns FALSE along with an error on failure.

PHP File Opening Modes: fopen() Modes

The following table lists and briefly describes all the file opening modes in PHP.

Mode Description
r opens the file for reading.
r+ opens the file for both reading and writing.
w opens the file for writing.
w+ opens the file for both writing and reading.
a opens the file for appending.
a+ opens the file for appending and reading.
x opens the file for writing.
x+ opens the file for both writing and reading.
c opens the file for writing.
c+ opens the file for both writing and reading.

Note: The w, w+, a, and a+ modes create a new file if the specified file does not exist.

Note: The a and a+ modes preserve previous content and write new content at the end of the file.

Note: The x and x+ modes return FALSE if the file already exists. This mode is used to avoid overwriting.

Note: The c and c+ modes create a new file if the specified file is not available or does not exist.

PHP fopen() Function Example

Consider the following PHP code as an example demonstrating the "fopen()" function:

<?php
   $myfile = "codescracker.txt";
   $fs = fopen($myfile, "r");
   $myline = fgets($fs);
   while($myline!=null)
   {
      echo $myline;
      echo "<br>";
      $myline = fgets($fs);
   }
   fclose($fs);
?>

The output of the above PHP example is:

php fopen function example

The same program can also be written as:

<?php
   $fs = fopen("codescracker.txt", "r");
   while(!feof($fs))
   {
      $x = fgets($fs);
      echo $x . "<br>";
   }
   fclose($fs);
?>

PHP Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!