PHP - Files & I/O

    PHP - Files & I/O

    PHP allows you to perform some file functions like opening a file, reading a file, writing a file and closing a file from within the program.

    1. Opening and closing file

    PHP supports the use of fopen() function to open a file that takes two parameters to be passed. The first parameter is the file name and the second parameter is in what mode you want to open the given file. There are 6 different modes that can be used for opening a file.

    Mode Purpose
    r The file will be opened in read-only mode and the cursor will be placed at the beginning of the file.
    r+ The file will be opened for reading and writing purpose and cursor will be at the beginning of the file.
    w The file will open in write-only mode and if the file does not exist then it will create one. The cursor will move to the beginning of file truncating it to zero length.
    w+ Opens file for reading and writing and if the file does not exist then it will create one. The cursor will move to the beginning of file truncating it to zero length.
    a The file will open in write-only mode and if the file does not exist then it will create one. The cursor will move to the end of the file.
    a+ The file will open in writing and reading mode and if the file does not exist then it will create one. The cursor will move to the end of the file.

    In case, opening a file will fail then it will return false else it will return the pointer to the file. Once you open a file for any purpose then it is necessary you close the file is available to another program can make changes to it. You can use fclose() function to close the file.

    2. Reading a file

    After opening a file using fopen() function, then you can read the file using fread() function which takes two parameters- file pointer and the length of the file in bytes. You can check the file length using filesize() function which takes a parameter which is the file name and return the result in bytes. You can use these function in this flow- fopen(), filesize(0, fread(), fclose.

    Example

    <html>
       <head>
          <title>Opening file in reading mode</title>
       </head>
       <body>      
          <?php
             $file_name = "file1.txt";
             $file = fopen( $file_name, "r" );         
             if( $file == false ) {
                echo ( "Error in opening file" );
                exit();
             }         
             $filesize = filesize( $file_name );
             $filetext = fread( $file, $filesize );
             fclose( $file );        
             echo ( "size of the file : $filesize bytes" );
             echo ( "<pre>$filetext</pre>" );
          ?>
       </body>
    </html>

    You can run the above example for the understanding of the code.

    3. Writing a file

    You can easily write to a file or append the existing file using the PHP fwrite() function. You can use two parameters with this function- pointer to the file and data to be written. You can consider using the third parameter that specifies the length of the data to be written. If a new file is created then after closing the file you can check the existence of the file using file-exist() function which takes a filename as the parameter.

    Example -

    <?php
       $file_name = "newfile.txt";
       $file = fopen( $file_name, "w" );   
       if( $file == false ) {
          echo ( "Error" );
          exit();
       }
       fwrite( $file, "This is  a simple file\n" );
       fclose( $file );
    ?>

    People are also reading: