Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP Filesystem readfile() Function



The PHP Filesystem readfile() function is used to read a file and write it to the output buffer. This function can return the number of bytes read on success, or false and an error on failure. We can hide the error output by adding an '@' in front of the function name.

We can use the URL as a filename with this function if the fopen() function wrappers have enabled in php.ini file.

Syntax

Below is the syntax of the PHP Filesystem readfile() function −

int readfile ( string $filename [, bool $use_include_path = FALSE [, resource $context ]] )

Parameters

Below are the required and optional parameters of the readfile() function −

Sr.No Parameter & Description
1

$filename(Required)

It is the file to read.

2

$use_include_path(Optional)

Set this option to true to look for the file in the include_path as well. The include_path can be specified in php.ini.

3

$context(Optional)

It's a context stream resource. Context is a collection of settings that might change the behavior of a stream.

Return Value

The function readfile() returns the number of bytes read from the file on success, and FALSE on failure.

PHP Version

The readfile() function was first introduced as part of core PHP 4 and work well with the PHP 5, PHP 7 and PHP 8.

Example

Here is the basic example to see how the PHP Filesystem readfile() function is used to read a file given.

First write the content inside the sample.txt file. So below is the content written in the file.

Hello World!
Tutorialspoint
Tutorix
32

Now run the below PHP code to see the result of the readfile() function −

<?php
   echo "Read the content of sample.txt file:";
   echo readfile("/PhpProject/sample.txt");
?>

Output

Here is the outcome of the following code −

Read the content of sample.txt file:
Hello World!
Tutorialspoint
Tutorix
32

Example

Here is the another example to show the usage of readfile() function to handle the errors while using it.

<?php
   $filename = "/PhpProject/testfile.txt";

   // Check if the file exists
   if (file_exists($filename)) {
      // Read and display the content of the file
      readfile($filename);
   } else {
      echo "File does not exist.";
   }
?> 

Output

This will produce the following result −

File does not exist.

Example

Here is one more example to read the different file format like .jpg using the readfile() function.

<?php
   $filename = "/PhpProjects/image.jpg";

   // Check if the file exists
   if (file_exists($filename)) {
      // Set the content type header to display the image
      header('Content-Type: image/jpeg');
      
      // Read and output the image file
      readfile($filename);
   } else {
      echo "File does not exist.";
   }
?> 

Output

This will generate the below output −

This code will show the image on the screen specified in the filename.

Example

Here is one more example to use readfile() function for downloading the file after setting the headers to trigger the download.

<?php
   $filename = "/PhpProjects/myfile.pdf";

   // Check if the file exists
   if (file_exists($filename)) {
       // Set headers to trigger a download
       header('Content-Description: File Transfer');
       header('Content-Type: application/octet-stream');
       header('Content-Disposition: attachment; filename="'.basename($filename).'"');
       header('Expires: 0');
       header('Cache-Control: must-revalidate');
       header('Pragma: public');
       header('Content-Length: ' . filesize($filename));
       
       // Read and output the file
       readfile($filename);
       echo "The file has been downloaded."
       exit;
   } else {
       echo "File does not exist.";
   }
?> 

Output

This will lead to the following output −

The file has been downloaded.

Summary

The readfile() method is a built-in function to read the given file. It is very helpful for showing file content on a web page or allowing file downloads.

php_function_reference.htm
Advertisements