Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • PHP's file handling system functions.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 978
    Comment on it

    As PHP is a server side scripting language it does allow to create, access and manipulate files on the web server.
    Let's see how it works.

     

    The very first need to work with files is to open the file. For this purpose, there is a PHP function fopen() that allows us to open the file.

    Syntax:

     

    fopen(filename, mode)

     

    Parameters list:

    filename: Specifies the file name we want to open.
    mode: Specifies in which mode we need to open.


    Example:

      <?php
            $fhandle = fopen("example.txt", "r"); //opening example.txt file with read mode.
        ?>

    There are different modes available for file permissions that allow the access mode of file.

    Following are some:


    r     Reading only.

    r+  For reading and writing.

    w   It is to open a file in writing only with clearing the content of the file. If the file doesn't exist it will create a file.

    w+ It is to open a file in reading and writing with clearing the content of the file. If the file doesn't exist it will create a file.    

    a    Append mode which opens the file for writing only and preserves the last content also start writing from the end of the file. If the file doesn't exist it will create a file.

    a+ Append mode which opens the file for reading/writing and preserves the last content also start writing from the end of the file. If the file doesn't exist it will create a file.

     

    After performing the operation in the file we need to close the file for which fcolse() PHP file handling function is used.   

     

     <?php
    
        $file = "data.txt";
    
        // Open the file for reading
        $handle = fopen($file, "r") or die("ERROR: Cannot open the file");
    
        // Some code to be executed
    
        // Closing the file handle
    
        fclose($handle);
    
        ?>

     

    Using fread() conjugation with filesize() make the whole file read at once.

    <?php
    
        $file = "data.txt";
    
        // Open the file for reading
        $handle = fopen($file, "r") or die("ERROR: Cannot open the file");
    
            
    
        // Some code to be executed
        $content = fread($handle, filesize($file));
         fwrite($handle, $data) or die ("ERROR: Cannot write the file");
       
    
        // Closing the file handle
    
        fclose($handle);
    
        ?>
    
    

     

    Now, if we want to write data in the file we can use the fwrite(file handle, vstring) function.

    Example:

    <?php
    
        $file = "data.txt";
    
        $data = "This is the string to write in file.";
            
    
        $handle = fopen($file, "w") or die("ERROR: Cannot open the file");
    
        $content = fread($handle, filesize($file));
    
        fwrite($handle, $data) or die ("ERROR: Cannot write the file");
    
        fclose($handle);
    
    ?>

    Similarly, fread() is a function to read the file content. There are many other file handling functions in PHP.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: