CodesJava

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.

Syntax:

readfile(filename,include_path,context)

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.

Example:

<!DOCTYPE html>
<html>
<body>
 
<?php
echo readfile("phpfile.txt");
?>
 
</body>
</html>

Output:

Hello!I am the first line. 
And I am the second line.
Hey, I am the third one.
Posted in PHP   
Sign Up/ Sign In
Ask a Question


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki