Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    APIs
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> PHP >> File Handling in PHP
File Handling in PHP
File Handling in PHP


by Arun Singh on 9/13/2011 9:08:59 PM

Views: 4189       Comments: 0

File Handling in PHP

In this article I intend to learn basic file handling mechanism of PHP. File handling functions in PHP are extremely useful and userfriendly.PHP includes a lot of built-in functions for handling files and directories. You can read, write, delete, and get lots of information on files through the use of these functions.

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.

<!DOCTYPE html>

<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

        <title></title>

    </head>

    <body>

        <?php

           

          // Open file from specified path with read only mode

          $file= fopen("C://Apache Server//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 from specified path 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 from specified path 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 from specified path 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.

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

        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.

Report Abuse Form
Reason:    
 

Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by Arun SinghRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 2863
Advertisement
MindStick SurveyManager
Advertise with Us
  
Copyright © 2013MindStick. All Rights Reserved.