Home Tutorial Php Phpbasics Tutorial PHP File Handling

 
 

PHP File Handling
Posted on: November 5, 2009 at 12:00 AM
Advertisement
PHP File Handling: PHP supports file handling with fclose, fopen, feof etc. functions. This tutorial will let you know about every facet of file handling. The table will introduce you with the various mode of file opening.

PHP File Handling:

Every language supports File handling, file handling has its own importance, as we know that to make the output permanent we need to use file handling. PHP supports this feature using the following functions:

i)    fopen: To open a file in a particular mode, like r: read, w: write, a: append etc.

ii)    feof: To check whether the pointer has reached at the end of the file or not.

iii)    fclose: To close the file.

iv)    fgets: To extract the content line by line.

v)    fgetc: To extract the content character by character.

MODES DESCRIPTION
r read only
r+ read and write
w write only, clears the previous content, creates the file if it does not exist
w+ read/write, clears the previous content,  creates the file it is not present
a append, open and write the content to the end of the file, creates if the file is not present
a+ append/write,  open and write the content to the end of the file, creates if the file is not present
x write only, returns an error message and false value if the file is present otherwise creates the new file
x+ read/write , returns an error message and false value if the file is present otherwise creates the new file

 

Example 1(r-mode):

<?php

$file=fopen("PHP.txt","r")or exit("Can't open");

$a=0;

while(!feof($file))

{

    $string=fgets($file);

    echo $string;

    $string=explode(" ",$string);

    foreach($string as $str)

{

if($str == "OOP")

                    $a++;

                }

}

echo "<br/><b>Total no. of OOP word is:</b>". $a;

fclose($file);

?>

Output:

What is PHP? PHP is an acronym stand for Hypertext PreProcessor. It is an open source scripting language which is used for web development and can be embedded in HTML. PHP is free, efficient, and that's why it is very popular in web development scenario. PHP coding is enclosed within tag. The page originates as PHP code on the server but is transformed into HTML code before sending along to the PHP code and converts it into HTML code. History of PHP The development process of PHP was started in 1994 as a set of Common Gateway Interface binaries written in 'C'-language by Rasmus Lerdorf. It was released publicly on June 8, 1995. The variables, form handling process, and the ability to embed HTML coding is much like Perl. Two developers from Technion IIT of Israel, Zeev Suraski and Andi Gutmans rewrote the parser in 1997 and laid the base of PHP 3. The official launch of PHP 3 came in June 1998. Suraski and Gutmans then started a new rewrite of PHP's core, producing the Zend Engine in 1999. PHP 4 was released on May 22, 2004, it was powered by Zend engine 1.0. Then PHP 5 was released on July 13, 2004 powered by the new Zend Engine II. PHP 5 included better support for OOP and other enhancements. Late static binding was included in PHP 5.3. PHP 6 is under developed and it will be released soon, it will remove many functions which are present in PHP 5.3 and if any user wiill try to use those functions then a warning message will be generated.
Total no. of OOP word is:1

Example 2(w-mode):

<?php

$file=fopen("New.txt","w");

$string="This";

fwrite($file,$string);

$string="is";

fwrite($file,$string);

$string="new";

fwrite($file,$string);

$string="file";

fwrite($file,$string);

$file=fopen("New.txt","r");

while(!feof($file))

{

        echo fgets($file);

}

?>

Output:

Thisisnewfile

Example 3(a-mode):

<?php

$file=fopen("New.txt","a");

$string="This";

fwrite($file,$string);

$string="is";

fwrite($file,$string);

$string="append";

fwrite($file,$string);

$file=fopen("New.txt","r");

while(!feof($file))

{

        echo fgets($file)."<br/>";

}

?>

Output:

ThisisnewfileThisisappend

Example 4 (x-mode, if file exists):

<?php

$file=fopen("New.txt","x");

$string="This";

fwrite($file,$string);

$string="is";

fwrite($file,$string);

$string="new";

fwrite($file,$string);

$file=fopen("New.txt","r");

while(!feof($file))

{

    echo fgets($file)."<br/>";

}

?>

 

Output:


Warning:  fopen(New.txt) [function.fopen]: failed to open stream: File exists in C:\wamp\www\phpBasics\phpFile.php on line 3



Warning:  fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\www\phpBasics\phpFile.php on line 5



Warning:  fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\www\phpBasics\phpFile.php on line 7



Warning:  fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\www\phpBasics\phpFile.php on line 9

ThisisnewfileThisisappend

Example 5:

<?php

$file=fopen("Latest.txt","x");

$string="This";

fwrite($file,$string);

$string=" ";

fwrite($file,$string);

$string="is";

fwrite($file,$string);

$string=" ";

fwrite($file,$string);

$string="Latest.txt file";

fwrite($file,$string);

$file=fopen("Latest.txt","r");

while(!feof($file))

{

    echo fgets($file)."<br/>";

}

?>

Output:

This is Latest.txt file

Note: If you will  press the F5 button, you will get the following output:


Warning:  fopen(Latest.txt) [function.fopen]: failed to open stream: File exists in C:\wamp\www\phpBasics\phpFile.php on line 3



Warning:  fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\www\phpBasics\phpFile.php on line 5



Warning:  fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\www\phpBasics\phpFile.php on line 7



Warning:  fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\www\phpBasics\phpFile.php on line 9



Warning:  fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\www\phpBasics\phpFile.php on line 11



Warning:  fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\www\phpBasics\phpFile.php on line 13

This is Latest.txt file

 

 

Advertisement

Related Tags for PHP File Handling:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 5, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com