Sunday, September 24, 2017

PHP File Handling


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


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

Example :
<?php  
$handle = fopen("c:\\example\\file.txt", "r");  
?>
PHP Write File - fwrite():
The PHP fwrite() function is used to write content of the string into file.
Syntax:
int fwrite ( resource $handle , string $string [, int $length ] )

Example :
<?php  
$fp = fopen('data.txt', 'w');//open file in write mode  
fwrite($fp, 'Skptricks');  
fwrite($fp, 'Writing to file');  
fclose($fp);  
  
echo "File written successfully";  
?>

PHP 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:
string fread ( resource $handle , int $length )

Example :
<?php    
$filename = "c:\\file.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    
?>

PHP Close File - fclose():
The PHP fclose() function is used to close an open file pointer.
Syntax :
bool fclose ( resource $handle )
Example :
<?php  
fclose($handle);  
?>

PHP Delete File - unlink():
The PHP unlink() function is used to delete file.
Syntax:
bool unlink ( string $filename [, resource $context ] )
Example :
<?php    
unlink('file.txt');     
echo "File deleted successfully";  
?>



No comments:

Post a Comment