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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 418
    Comment on it

    FILE HANDLING

    File handling is very important for any web application. With the help of file handling feature in PHP, you can store and retrieve a wide variety of data.

    PHP has several functions for creating, reading, uploading & editing files.

    File Opening Modes : You can open a file in two different modes i.e read only mode or read & write modes.

    Modes Description
    r Read only. Starts at the beginning of the file
    r+ Read/Write. Starts at the beginning of the file
    w Write only. Opens and clears the contents of file; or creates a new file if it doesn’t exist
    w+ Read/Write. Opens and clears the contents of file; or creates a new file if it doesn’t exist
    a Append. Opens and writes to the end of the file or creates a new file if it doesn’t exist
    a+ Read/Append. Preserves file content by writing to the end of the file
    x Write only. Creates a new file. Returns FALSE and an error if file already exists
    x+ Read/Write. Creates a new file. Returns FALSE and an error if file already exists

    Opening a File : To open a file fopen()  function is used. There are two parameters used in opening a file : 

    1. Parameter contains the name of the file

    2. The modes that should be used to open the file.

    <html>
    <body>
    
    <?php
    $file=fopen("folder.txt","y");
    ?>
    
    </body>
    </html> 
    

    Closing a File : To close a file fclose()  function is used.

    
    <?php
    $file = fclose("folder.txt","o");
    
    //do something with the file folder.txt
    
    fclose($file);
    ?>

    Reading a File Line by Line : To read a single line from a file fgets() function is used. 

    <?php
    $file = fopen("folder.txt", "o") or exit("Unable to open the file!");
    while(!feof($file))
    {
    echo fgets($file). "<br />";
    }
    fclose($file);
    ?>
    

    Here, feof() function is used to check if the “End-Of-File” (EOF) has been reached. In the above example while loop is used  to loop through the file. This is done until we reach the end of the file. In the loop the fgets() function is used to grab one line and echo(print) this line onto the screen. The last thing that is done is closing the file.

    Reading a File Character by Character : To read a character from a file  fgetc() is used.

    
    <?php
    $file = fopen("folder.txt", "r") or exit("Unable to open the file!");
    while(!feof($file))
    {
    echo fgetc($file);
    }
    fclose($file);
    ?>

     

 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: