Thursday, 16 April 2015

File-handling-in-PHP


(1)Reading a file  example:

First way:


<?php


$fhandle=fopen("abc.txt","r");


$line=fread($fhandle,filesize("abc.txt"));


echo $line;


?>

Second Way:

<?php

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


//Output a line of the file until the end is reached


while(! feof($file))

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


?>

(2)Writing a file example it will override the file content when you run it:


<?php

$fhandle=fopen("abc.txt","w");

fwrite($fhandle,"this is my file om maurya");

?>


(3)writing a file example it will append  this line at he  end of  file:

<?php

$fhandle=fopen("abc.txt","a");


fwrite($fhandle,"this is my file om maurya");



?>




No comments:

Post a Comment