PHP Open File

PHP fopen() function is used to open file or URL and returns resource. The fopen() function accepts two arguments: $filename and $mode. The $filename represents the file to be opended and $mode represents the file mode for example read-only, read-write, write-only etc.

Syntax

  1. resource fopen ( string $filename , string $mode [, bool $use_include_path = false [, resource $context ]] )  

PHP Open File Mode

ModeDescription
rOpens file in read-only mode. It places the file pointer at the beginning of the file.
r+Opens file in read-write mode. It places the file pointer at the beginning of the file.
wOpens file in write-only mode. It places the file pointer to the beginning of the file and truncates the file to zero length. If file is not found, it creates a new file.
w+Opens file in read-write mode. It places the file pointer to the beginning of the file and truncates the file to zero length. If file is not found, it creates a new file.
aOpens file in write-only mode. It places the file pointer to the end of the file. If file is not found, it creates a new file.
a+Opens file in read-write mode. It places the file pointer to the end of the file. If file is not found, it creates a new file.
xCreates and opens file in write-only mode. It places the file pointer at the beginning of the file. If file is found, fopen() function returns FALSE.
x+It is same as x but it creates and opens file in read-write mode.
cOpens file in write-only mode. If the file does not exist, it is created. If it exists, it is neither truncated (as opposed to ‘w’), nor the call to this function fails (as is the case with ‘x’). The file pointer is positioned on the beginning of the file
c+It is same as c but it opens file in read-write mode.

PHP Open File Example

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

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 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

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

PHP Close File – fclose()

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

Syntax

  1.  fclose ( resource $handle )  

Example

<?php  
fclose($handle);  
?>  

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

  1. string fread ( resource $handle , int $length )  

Example

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

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

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

 

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

<?php    
unlink('data.txt');  
   
echo "File deleted successfully";  
?>