PHP File Handling (I/O)

PHP File Handling


Hello, folks! welcome back to a new section of our tutorial on PHP. In this section of our PHP tutorial, we will be studying about the PHP File handling (I/O).

In this tutorial, we will be giving an in-depth explanation to the different PHP functions related to files -

  • Opening a File
  • Reading a File
  • Writing a File
  • Closing a File

Opening and Closing a File

The fopen() function is used to open a file. This function needs two arguments stating first the file name and then mode in which to operate.

The file mode can be specified as one of the six available options in the table below -

Sr.NoMode & Purpose
1

r

Opens the file for reading only.

Places the file pointer at the beginning of the file.

2

r+

Opens the file for reading and writing.

Places the file pointer at the beginning of the file.

3

w

Opens the file for writing only.

Places the file pointer at the beginning of the file.

and truncates the file to zero length. If files does not

exist then it attempts to create a file.

4

w+

Opens the file for reading and writing only.

Places the file pointer at the beginning of the file.

and truncates the file to zero length. If files does not

exist then it attempts to create a file.

5

a

Opens the file for writing only.

Places the file pointer at the end of the file.

If files does not exist then it attempts to create a file.

6

a+

Opens the file for reading and writing only.

Places the file pointer at the end of the file.

If files does not exist then it attempts to create a file.



If an attempt to open a file fails, then the fopen() function returns a value of false otherwise it returns a file pointer which is used for further reading or writing to that file.

After making all the changes to the opened file, it is vital to close it by using the fclose() function. The fclose() requires a file pointer as its argument and then returns true when the closure succeeds and false if it fails.

PHP readfile

Reading a File

Once a file is opened by making use of the fopen() function, it can be read by using the fread() function. This function requires two arguments. These must be the file pointer and length of the file expressed in bytes.

The file length can be found by using the PHP filesize() function which takes the file name as its argument and returns the size of the file expressed in bytes.

Following are the steps needed to read a file with PHP -

  • Open a file using the fopen() function.
  • Get the length of the file by using the filesize() function.
  • Read the file's content using the fread() function.
  • Close the file by making use of the fclose() function.


Example

The following example below assigns the content of a text file to a variable and then displays those content on the web page.

<html>

   <head>
      <title>Reading a file using PHP</title>
   </head>
   
   <body>
      
      <?php
         $filename = "tmp.txt";
         $file = fopen( $filename, "r" );
         
         if( $file == false ) {
            echo ( "Error in opening file" );
            exit();
         }
         
         $filesize = filesize( $filename );
         $filetext = fread( $file, $filesize );
         fclose( $file );
         
         echo ( "File size : $filesize bytes" );
         echo ( "<pre>$filetext</pre>" );
      ?>
      
   </body>
</html>

Output

When the above code is executed, it will produce the following result -



Write a File in PHP

Writing a File

A new file can be written or text appended to an existing file making use of the fwrite() function. This PHP function requires two arguments specifying a file pointer and the string of data that is to be written. Optionally you can put a third integer arguments to specify the length of data to write. If you put the third argument, writing will stop after the specified length has been reached.

Example

The example below creates a new text file then writes a short heading inside it. After closing this file, its existence is confirmed by using the file_exist function which takes the file name as an argument -

<?php
   $filename = "/home/user/guest/newfile.txt";
   $file = fopen( $filename, "w" );
   
   if( $file == false ) {
      echo ( "Error in opening new file" );
      exit();
   }
   fwrite( $file, "This is  a simple test\n" );
   fclose( $file );
?>
<html>
   
   <head>
      <title>Writing a file using PHP</title>
   </head>
   
   <body>
      
      <?php
         $filename = "newfile.txt";
         $file = fopen( $filename, "r" );
         
         if( $file == false ) {
            echo ( "Error in opening file" );
            exit();
         }
         
         $filesize = filesize( $filename );
         $filetext = fread( $file, $filesize );
         
         fclose( $file );
         
         echo ( "File size : $filesize bytes" );
         echo ( "$filetext" );
         echo("file name: $filename");
      ?>
      
   </body>
</html>

Output

When the above code is executed, it will produce the following result -



Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial, we are going to be studying about the PHP Functions.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.