PHP Read and write files

Php fread() function used to reads a string of characters from a file

php fwrite() function writes a string of characters to a file


chech End-of-file

the php feof() method checks if the 'end-of-file' (eof) has been reached.

the feof() function return true when the file pointer has reached the end of the file and returns false otherwise.

note : feof() is useful with fread() or fgetc() in a while loop when you don't know how long the file is:

       if( feof($file)) echo "pointer reach end of file"; 

PHP -reading a file character by character

the php getc() function read a single character from a file.

this method returns just one character from the file it points to, it returns false when file pointer reached the end of the file.

         $char = fgetc( $file );

note : fgetc() function is slow when working with large files.

note : after a call to this method the file pointer moves to the next character.

in this example blow reads a file character by character, until the end of file is reached:

  <?php
            $file = fopen("/data.html", "r");
            
                     while( !feof($file) ){    
                    
                                                     $char = fgetc( $file );
                         
                                                     echo  "$char <br/>";
                         
                                                  }  fclose( $file );
  ?>

the fread() function can be used to read a string of characters from a file.

           fread( $file , chars)

chars : specifies the number of characters you want to read from a file at a time.

note : this function is faster then the fgetc() function when you working with large files.

  <?php
            $file = fopen("http://www.ipaddres.com/data.html", "r");
            
                     while( !feof($file) ){    
                    
/*read ten characters and returns them as a string*/ $char = fread( $file, 10 );
                         
                                                     echo  "$char <br/>";
                         
                                                  }  fclose( $file );
  ?>

php - reading a file line by line

the php fgets() function read a single line from a file.

if there is no more data to read in the file pointer, then false is returned.

     <?php    $handle = fopen("/data.text", "r");
     
                 if($handle){
                 
                             while( ($buffer = fgets($handle, 4090))   !== false){
                             
                                     echo  $buffer;
                                  
                                  }
                 if(!feof($handle)){
                 
                                     echo "error: unexpected fgets() fail \n";
                                  }
                                  
                                  fclose($handle);
                        }
     ?>

PHP Writing a file

the fwrite() function used to write binary data to a file.

this function returns the number of bytes written, or false on failure.

      fwrite( file, text, length)
  • file : required. specifies the open file to write to
  • text : required. specifies the string to write to the open file
  • length: optional. specifies the maximum number of bytes to write.
  <?php     
             $file = fopen( "/data.html", "w");
             
             $v    = fwrite( $file, "my name is amazon");
             
             echo   "$v";
             
             echo   fwrite( fopen( "/demo.html", "w"),  "this is the ram", 10);
  ?>

PHP - Copying Renaming and Deleting Files

the copy() function copies a file.

this function returns true on success and false on failure.

syntax:

          copy( source, dest)
  • source : path to the source file.
  • dest    : the destination path.

the rename() function renames a file or directory.

           rename( oldname, newname)

the unlink() function deletes a file.

           unlink( filename )
    <?php   
    
              $file = "data.html";
              
              $newfile = "demo.html";
              
              
              if(!copy($file, $newfile)){
                 
                              echo "failed to copy \n";
                              
                            }
                            
              if(!rename( $file, $newfile)){
              
                              echo "failed to rename  \n";
                           
                            }
              
              if(!unlink( $newfile)){
              
                              echo "failed to rename \n";
                              
                            }
                            
     ?>