LERAN PHP COMPLETE

Learn one of the most powerful programming languages in the world and become a rockstar developer.

PHP Files and I/O


This chapter will explain following functions related to files −

  • Opening a file
  • Reading a file
  • Writing a file
  • Closing a file

Opening and Closing Files

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

Files modes can be specified as one of the six options in this table.

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 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 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 a changes to the opened file it is important to close it with the fclose() function. The fclose() function requires a file pointer as its argument and then returns true when the closure succeeds or false if it fails.

Reading a file

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

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

Write to File

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

x
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Privacy Policy That's Fine