PHP File I/O

You can use a number of standard functions to perform file I/O including:

Check out also the series of functions like is_readable and is_writable that checks permissions. Also, fflush is a useful command to flush file I/O commands. Check out a complete list at php.net under Filesystem functions.

Here is some example PHP code to open a file and read one line at a time from the file and finally closes the pointer to the file.

$file_handle = fopen("myfile", "r");  
while (!feof($file_handle)) {     
  $line = fgets($file_handle);     
  echo $line;  }  
fclose($file_handle);
Try it out now


The next set of code reads the entire file

     $fh = fopen("readfile.txt", "rb");
     $data = fread($fh, filesize("myfile"));
     fclose($fh);
   

 

 
© Lynne Grewe