Easy learning with example program codes

PHP Download File

Downloading a file is possible in PHP with PHP readfile() function. PHP readfile() function is a built in function already defined in PHP library.

PHP readfile() function:

PHP readfile() function is used to read a file and to write it to the output buffer. In case of a successful interaction, PHP readfile() function returns the number of bytes of the file read, while in case of a failure, a FALSE or an error is returned.



Filename:  This is a mandatory parameter which specifies the name of the file to read.

Include_path: This is an optional parameter which is used to search a file in the included path, if the value of this parameter is set to “1” or “TRUE”. The default value of include-path is FALSE.

Context: This is an optional parameter which specifies the context (a set of options which is used to modify the behavior of a stream) of the file handle. In other words this parameter is a context stream resource.

Features of PHP readfile() function:

  • An URL can also be used as a filename in PHP readfile() function, by enabling the fopen wrappers in the php.ini file.
  • In case of a failure in reading a file and writing the file to the output buffer, an ‘@’ symbol can be used before the function name to hide the error output.


<!DOCTYPE html>
echo readfile("phpfile.txt");


Hello!I am the first line. 
And I am the second line.
Hey, I am the third one.
Posted in PHP   

Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at


We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on

Copyright © 2019 CodesJava Protection Status SiteMap Reference: Java Wiki