PHP File Handling

PHP File System allows us to create file, read file line by line, read file character by character, write file, append file, delete file and close file.

PHP provides fopen(), fclose(), fread(), fwrite(), unlink() function to manipulates files.

Please find the PHP File Handling function as per as given below.

PHP Open File - fopen()

The PHP fopen() function is used to open a file.

PHP Syntax:
fopen ( string $filename , string $mode [, bool $use_include_path = false [, resource $context ]] );

PHP Example :

File : learnphp.txt
Welcome to aryatechno!
Learn php tutorials.

<?php  
      $handle = fopen("learnphp.txt", "r");  //open file in read mode 
?>

 

PHP Read File - fread()

The fread() function reads from an open file.

The first parameter of fread() contains the name of the file to read from and the second parameter specifies the maximum number of bytes to read.

PHP Syntax:
    string fread ( resource $handle , int $length ) 

PHP Example :

File : learnphp.txt
Welcome to aryatechno!
Learn php tutorials.

<?php  
     $filename="learnphp.txt";
      $handle = fopen($filename, "r");  //open file in read mode    
      $contents = fread($handle, filesize($filename)); //read file    
      echo $contents; //printing data of file  
      fclose($handle); //close file    
  ?>   

Output:
Welcome to aryatechno!
Learn php tutorials.

PHP Write File - fwrite()

The PHP fwrite() function is used to write content of the string into file.
PHP Syntax:

    int fwrite ( resource $handle , string $string [, int $length ] ) 

PHP Example :

<?php  
    $filename="learnphp.txt";
    $fp = fopen($filename, 'w'); //open file in write mode  
    fwrite($fp, 'Learn PHP File handling ');  //write contents in file
    fclose($fp);  //close file
    echo "File has been written successfully";  
    ?> 

Output:
File has been written successfully

PHP Close File - fclose()

The fclose() function is used to close an open file.

PHP Syntax:
fclose(resource $handle);

PHP Example :

<?php  
    fclose($handle);  
?> 

PHP Delete File - unlink()

The PHP unlink() function is used to delete file.

PHP Syntax:
    bool unlink(string $filename [, resource $context ]) 

PHP Example :

<?php
    unlink(learnphp.txt'); // Remove file from directory
    echo "File deleted successfully";  
?>

Output:
File deleted successfully


Modes are given below.

  1. r => Open a file for read only. File pointer starts at the beginning of the file
  2. w => Open a file for write only. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file
  3. a => Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist
  4. x => Creates a new file for write only. Returns FALSE and an error if file already exists
  5. r+ => Open a file for read/write. File pointer starts at the beginning of the file
  6. w+ => Open a file for read/write. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file
  7. a+ => Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist
  8. x+ => Creates a new file for read/write. Returns FALSE and an error if file already exists

 

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

74450