PHP readfile() Function

Definition

The readfile() function reads a file and writes it to the output buffer.

Syntax

PHP readfile() Function has the following syntax.

readfile(filename,include_path,context)

Parameter

Parameteris RequiredDescription
filenameRequired.File to read
include_pathOptional.'1' means to search for the file in the include_path in php.ini
contextOptional.Context of the file handle.

Return

This function returns the number of bytes read on success, or FALSE and an error on failure.

You can hide the error output by adding an '@' in front of the function name.

Example

When passed a filename as readfile()'s only parameter, readfile() will attempt to open it, read it all into memory.

If successful, readfile() will return an integer equal to the number of bytes read from the file.

If unsuccessful, readfile() will return false.

Here is an example script:


<?PHP/*from  w  w  w. j a  va 2s.co m*/
      $testfile = @readfile("test.txt");
      if (!$testfile) {
             print "Could not open file.\n";
      }
?>

If readfile() fails to open the file, it will print an error message to the screen. You can suppress this by placing an @ symbol before the function call.

The code above generates the following result.





















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions