PHP Download File

PHP enables you to download file easily using built-in readfile() function. The readfile() function reads a file and writes it to the output buffer.


PHP readfile() function

Syntax

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

$filename: represents the file name

$use_include_path: it is the optional parameter. It is by default false. You can set it to true to the search the file in the included_path.

$context: represents the context stream resource.

int: it returns the number of bytes read from the file.


PHP Download File Example: Text File

  

<?php  
   $file_url = 'http://www.tutorialspro.com/f.txt';  
   header('Content-Type: application/octet-stream');  
   header("Content-Transfer-Encoding: utf-8");   
   header("Content-disposition: attachment; filename=\"" . 
   basename($file_url) . "\"");   
   readfile($file_url);  
?>

PHP Download File Example: Binary File

<?php  
   $file_url = 'http://www.myremoteserver.com/file.exe';  
    header('Content-Type: application/octet-stream');  
   header("Content-Transfer-Encoding: Binary");   
   header("Content-disposition: attachment; filename=\"" . 
   basename($file_url) . "\"");   
   readfile($file_url);  
?>  

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