Friday, September 25, 2020

PHP File Handling (I/O)



Hello, dear readers! welcome back to another section of my 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 are going to be giving a proper explanation of 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 the 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.



Once an attempt to open a file fails then fopen() 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.


Reading a File

Once a file is opened by using the fopen() function, it can be read by making use of the fread() function. This PHP 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.

RECOMMENDED: Strings in PHP

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 -


RECOMMENDED: Loops in PHP


Writing a File

A new file can be written or text can be appended to an existing file by making use of the fwrite() function. This function requires two arguments specifying a file pointer & the string of data to be written. You can also include an optional third integer arguments to specify the length of data to write. If you included the third argument, writing will stop after the specified length has been reached.

Example
The following example creates a new text file then writes a short heading inside of 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 my next tutorial post, we will be studying about the PHP Functions.

Feel free to ask your questions where necessary and i 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.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.