articles

Home / DeveloperSection / Articles / File Handling in PHP

File Handling in PHP

Anonymous User21657 13-Sep-2011

File handling is an important part of any web application. You often need to open and process a file for different tasks.

Let’s we have an examples, how to read, write, append, delete, close and copy a file in PHP. 

Opening a File:

The fopen() function is used to open a files in PHP.

Syntax:

fopen($filename, $mode);

Here, $filename specifies the file name to be opened and $mode specifies the mode in which file should be opened.

Example:

In the following example, if file could not be open then it will display ‘Unable to open file’ message.

<!DOCTYPEhtml>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
        <title></title>
    </head>
    <body>
        <?php
           
          // Open file fromspecifiedpath with read only mode
          $file= fopen("C://ApacheServer//htdocs//MySiteFile//test.txt", "r")
          or
          exit('Unable to open file');
         
        ?>
    </body>
</html>
Reading a File:

There are lots of method to reading file in PHP such as: fread(), fgets(),fgetc() etc.

Now let’s see the basic difference between them.

Reading a file line by line:

fgets() function used for reading file line by line in PHP.

Example:
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
        <title></title>
    </head>
    <body>
        <?php
           
          // Open file fromspecifiedpath with read only mode
          $file= fopen("C://Users//Sachindra//Desktop//wamp.txt", "r") or exit('Unable to open file');
         
         // use feof() function to check end of file
         
         while (!feof($file)) //this condition check end of file
         {
             echo fgets($file);
         }
         
         echo '</br>'.'<h3>'.'File reading line by line compelete.'.'</h3>';
        ?>
    </body>
</html>

 

Output:

File Handling in PHP

After completion of reading file, ‘File reading line by line complete’ message will be displayed.

Reading a file character by character:

fgetc() function is used for reading file character by character.

Example:
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
        <title></title>
    </head>
    <body>
        <?php
           
          // Open file fromspecifiedpath with read only mode
          $file= fopen("C://Users//Sachindra//Desktop//wamp.txt", "r") or exit('Unable to open file');
         
         // use feof() function to check end of file
         
         while (!feof($file)) //this condition check end of file
         {
            // Reading file character by character
             echo fgetc($file);
         }
         
         echo '</br>'.'<h3>'.'File reading character by character compelete.'.'</h3>';
        ?>
    </body>
</html>

 

Output:

File Handling in PHP

Reading a file up to specified length:

fread() function is used for file reading up to specified length.

Syntax:

fread($handle, $length) ;

Here, $handle specifies the file name to be read and $length specifies the length, up to read the file.

Example:
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
        <title></title>
    </head>
    <body>
        <?php
           
            
          // Open file fromspecifiedpath with read only mode
          $file= fopen("C://Users//Sachindra//Desktop//wamp.txt", "r") or exit('Unable to open file');
         
          // filesize function retruns length of file
          $content = fread($file, 100);
          echo $content;
          echo '<h3>'.'</br>'.'File reading complete up to length 100 !'.'</h3>';
      
        ?>
    </body>
</html>
Output:

File Handling in PHP

Copying and Deleting files:

Copy () function is used for copy one file to another one whereas unlink () function is used for delete files.

Let’s we have an example, how to copy and delete files in PHP.

Example: Copy file from source file to target file.

<!DOCTYPEhtml>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
        <title></title>
    </head>
    <body>
        <?php
        // "C://Users//Sachindra//Desktop//wamp.txt" source file path
        copy("C://Users//Sachindra//Desktop//wamp.txt", "target.txt");
        echo 'file copied successfully';
           
        ?>
    </body>
</html>
Output:

File Handling in PHP

Now you can see ‘target.txt’ in source folder where your above file is stored.


File Handling in PHP

Example: Delete specified files.

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
        <title></title>
    </head>
    <body>
        <?php
        // "C://Users//Sachindra//Desktop//wamp.txt" source file path
        unlink("target.txt");
        echo 'file deleted successfully';
           
        ?>
    </body>
</html>
Output:

File Handling in PHP

Now file successfully deleted from source folder.


Updated 21-Jan-2020
I am a content writter !

Leave Comment

Comments

Liked By