PHP

PHP-Files & I/O

PHP-Files & I/O

Opening and closing files –  

The function fopen() is used to open a file. Two arguments are required first the file name and second the mode in which it operates.

Sr.No

Mode & 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 do 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 do 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 do 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 do not exist then it attempts to create a file.

In case the attempt to open a file fails then the fopen() will return a false value otherwise a file pointer is used for reading and writing the file further. To close the file fclose ()  function is used. If the closure of the file succeeds the value returned is true or else if not then false 

  • Reading a file –  the function fread() is used to read the file that is opened. It requires two arguments. File pointer and the length of the file that is expressed in bytes.

The filesize() function is used to measure the length of the file. This function takes the file name as the argument and the size of the file is returned in bytes.

Steps to read a file – 

  • Open file by using fopen() function.
  • Measure the length of the file by the fsize() function.
  • Read the content of the file by fread() function.
  • Close the file by fclose() function.

Example – the content in the text file is assigned to a variable and then displayed on the web page.

Input

<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- 

  • Writing a file – The fwrite() function is used to write in an existing file. It requires two arguments file pointer and the string of data that is to be written. To specify the length of the data that is to be written a third integer argument is included. If this is included the writing of data will be stopped if a specified length is reached. 

After a file is closed the existence of the file can be confirmed using the file_exist() function.

Example – 

Input – 

<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- 

  • Writing a file – The fwrite() function is used to write in an existing file. It requires two arguments file pointer and the string of data that is to be written. To specify the length of the data that is to be written a third integer argument is included. If this is included the writing of data will be stopped if a specified length is reached. 

After a file is closed the existence of the file can be confirmed using the file_exist() function.

Example – 

Input – 

<?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>