Free download web templates, books and more. Free php projects ,games and apps. GTA mods and more

Breaking

Php File Handling || how to handle php files

PHP File Handling Tutorial


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.

Open File - fopen()

The PHP fopen() function is used to open a file.
Syntax
  1. resource fopen ( string $filename , string $mode [, bool $use_include_path = false [, resource $context ]] )  

Example
  1. <?php  
  2. $handle = fopen("c:\\folder\\file.txt""r");  
  3. ?>  

Close File - fclose()

The PHP fclose() function is used to close an open file pointer.
Syntax
  1. ool fclose ( resource $handle )  
Example
  1. <?php  
  2. fclose($handle);  
  3. ?>  

Read File - fread()

The PHP fread() function is used to read the content of the file. It accepts two arguments: resource and file size.
Syntax
  1. string fread ( resource $handle , int $length )  
Example
  1. <?php    
  2. $filename = "c:\\myfile.txt";    
  3. $handle = fopen($filename"r");//open file in read mode    
  4.   
  5. $contents = fread($handlefilesize($filename));//read file    
  6.   
  7. echo $contents;//printing data of file  
  8. fclose($handle);//close file    
  9. ?>    
Output
hello php file

PHP Write File - fwrite()

The PHP fwrite() function is used to write content of the string into file.
Syntax
  1. int fwrite ( resource $handle , string $string [, int $length ] )  
Example
  1. <?php  
  2. $fp = fopen('data.txt''w');//open file in write mode  
  3. fwrite($fp'hello ');  
  4. fwrite($fp'php file');  
  5. fclose($fp);  
  6.   
  7. echo "File written successfully";  
  8. ?>  
Output
File written successfully

PHP Delete File - unlink()

The PHP unlink() function is used to delete file.
Syntax
  1. bool unlink ( string $filename [, resource $context ] )  
Example
  1. <?php    
  2. unlink('data.txt');  
  3.    
  4. echo "File deleted successfully";  
  5. ?>