File & I/O In PHP

Admin   PHP   331  2020-11-06 21:50:05

This chapter will explain file related functions in PHP:

Open the file
Read the file
Record file
Close the file

Opening and closing files in PHP

PHP fopen() function is used to open a file. It requires 2 parameters, the first parameter is the file name, and the second parameter is the mode, ie the mode to operate.

The mode of the file can be specified as one of the six options in the following table.

Mode Purpose
r

Open file and read-only.

Move the file pointer to the beginning of the file

 
r+

Opens the file for reading and writing.

Move the cursor to the beginning of the file

in

Open file and write only.

Move the cursor to the beginning of the file

and cut the file to 0 in length.

If the file does not exist, it will attempt to create one.

in +

Opens the file for reading and writing.

Move the cursor to the beginning of the file

and cut the 0 file.

If the file does not exist, it will attempt to create one.

a

Open file for write-only purposes.

Move the cursor to the end of the file

If the file does not exist, it will attempt to create one.

a+

Open file for read and write purposes.

Move the cursor to the end of the file

If the file does not exist, it will attempt to create one.

If an attempt to open a file fails, then the fopen function returns a false value, otherwise it returns a file pointer used for continuing to read or write the file.

After making a change to the opened file, it's important to close it using the fclose() function. The fclose() function asks a file pointer as its argument and then returns true if the closure was successful and false otherwise.

Reading a file in PHP

When a file is opened using the fopen() function, it can be read with a fread() function in PHP. This function requires 2 parameters. They must be file pointers and the length of the file in bytes.

The length of the file can be determined using the filesize() function in PHP, which takes the filename as an argument and returns the size of the file in bytes.

Follow these steps to read a file with PHP:

Open the file using the fopen() function .

Get the file length using the filesize() function.

Read the contents of the file using the fread() function.

Close the file using the fclose() function.

Let's say you have a tmp.txt with the following content:

Save the above program in a file called test.php in htdocs, then open a browser and type http://localhost:8080/test.php will produce the following result:

Read and write files in PHP

The example below assigns the content of a text file to a variable, then displays its content on the web page.

<html>

   <head>
      <title> Reading the file in PHP </title>
   </head>
   
   <body>
      
      <?php
         $filename = "tmp.txt";
         $file = fopen($filename,"r");
         
         if ($file == false)
         {
            echo("An error occurred when opening file !!!");
            exit();
         }
         
         $filesize = filesize($filename);
         $filetext = fread($file, $filesize);
         fclose($file);
         
         echo ("The size of the file is: $filesize byte <br>");
         echo ("Below is the content of the file: <br>");
         echo ("<pre> $filetext </pre>");
      ?>
      
   </body>
</html>

Save the above program in a file called test.php in htdocs, then open a browser and type http://localhost:8080/test.php will produce the following result:

Reading a file in PHP

Write the file in PHP

A new file can be written or text can be appended to an existing file using PHP fwrite() function. This function requires two parameters: the file pointer and the data string to be written. An optional third integer parameter can be added to specify the length of the data to be written. If the third parameter is added, writing will stop after the specified length has been reached.

The following example creates a new text file, and writes a short text in its header. After closing this file, the existence of this file is confirmed by the file_exist() function, which will take the filename as an argument.

<? php
   $filename = "tmp.txt";
   $file = fopen($filename, "w");
   
   if ($file == false)
   {
      echo("An error occurred when opening file");
      exit();
   }
   fwrite($file, "Example of writing file in PHP. \ n");
   fclose($file);
?>


 

<html>
   
   <head>
      <title> Writes the file in PHP </title>
   </head>
   
   <body>
      
      <?php
         $filename = "tmp.txt";
         $file = fopen($filename, "r");
         
         if ($file == false)
         {
            echo("An error occurred when opening file");
            exit();
         }
         
         $filesize = filesize($filename);
         $filetext = fread($file, $filesize);
         
         fclose($file);
         
         echo("The size of the file is: $filesize byte <br>");
         echo("File name is: $filename <br>");
         echo("Below is the content of the file: <br>");
         echo("$filetext");
         
      ?>
      
   </body>
</html>

Save the above program in a file called test.php in htdocs, then open a browser and type http://localhost: 8080/test.php will produce the following result:

Write the file in PHP

And now the content of tmp.txt will be:

Read Write file in PHP

We will discuss all functions related to input and output files in the chapter on File Handling Functions in PHP