PHP Append to File

You can append data into file by using a or a+ mode in fopen() function. Let’s see a simple example that appends data into data.txt file.

Let’s see the data of file first.

welcome to php file write

PHP Append to File – fwrite()

The PHP fwrite() function is used to write and append data into file.

Example

 

<?php  
$fp = fopen('data.txt', 'a');//opens file in append mode  
fwrite($fp, ' this is additional text ');  
fwrite($fp, 'appending data');  
fclose($fp);  
  
echo "File appended successfully";  
?>

Output:

welcome to php file write this is additional text appending data

PHP Write File

PHP fwrite() and fputs() functions are used to write data into file. To write data into file, you need to use w, r+, w+, x, x+, c or c+ mode.

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');//opens file in write-only mode  
fwrite($fp, 'welcome ');  
fwrite($fp, 'to php file write');  
fclose($fp);  
  
echo "File written successfully";  
?> 

Output: data.txt

welcome to php file write

PHP Overwriting File

If you run the above code again, it will erase the previous data of the file and writes the new data. Let’s see the code that writes only new data into data.txt file.

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

Output:

hello

PHP Read File

PHP provides various functions to read data from file. There are different functions that allow you to read all file data, read data line by line and read data character by character.

The available PHP file read functions are given below.

  • fread()
  • fgets()
  • fgetc()

PHP Read File – fread()

The PHP fread() function is used to read data of the file. It requires two arguments: file resource and file size.

Syntax

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

$handle represents file pointer that is created by fopen() function.

$length represents length of byte to be read.

Example

<?php    
$filename = "c:\\file1.txt";    
$fp = fopen($filename, "r");//open file in read mode    
  
$contents = fread($fp, filesize($filename));//read file    
  
echo "<pre>$contents</pre>";//printing data of file  
fclose($fp);//close file    
?>

Output

this is first line
this is another line
this is third line

PHP Read File – fgets()

The PHP fgets() function is used to read single line from the file.

Syntax

  1. string fgets ( resource $handle [, int $length ] )  

Example

<?php    
     $fp = fopen("c:\\file1.txt", "r");//open file in read mode    
     echo fgets($fp);  
     fclose($fp);  
?> 

Output

this is first line

PHP Read File – fgetc()

The PHP fgetc() function is used to read single character from the file. To get all data using fgetc() function, use !feof() function inside the while loop.

Syntax

  1. string fgetc ( resource $handle )  

Example

<?php    
    $fp = fopen("c:\\file1.txt", "r");//open file in read mode    
     while(!feof($fp)) {  
       echo fgetc($fp);  
          }  
   fclose($fp);  
?>    

Output

this is first line this is another line this is third line

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";  
?>