Backend Development 4 min read

Using PHP readfile() to Output File Contents to the Browser or Another File

This article explains the PHP readfile() function, its syntax, return values, and provides step‑by‑step examples for displaying a text file in the browser, forcing a download, and copying its contents to another file using appropriate HTTP headers.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP readfile() to Output File Contents to the Browser or Another File

In PHP, the readfile() function is a very convenient function that can be used to output file contents to the browser or a file. The syntax of readfile() is as follows:

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

The readfile() function takes a filename as a parameter, reads the file’s contents, and outputs them directly. It returns the number of bytes read, or false on failure.

Below we look at an example, assuming we have a text file named data.txt with the following contents:

Hello, World!
I am learning PHP.

Now we want to output the file’s contents to the browser using PHP code. We can use readfile() to achieve this; the example code is as follows:

<?php
$file = 'data.txt'; // file path

if (file_exists($file)) {
    header('Content‑Disposition: attachment; filename=' . basename($file)); // download file
    header('Content-type: text/plain'); // set MIME type to plain text
    readfile($file); // output file contents
} else {
    echo "File does not exist.";
}
?>

In the above code we first check whether the file exists; if it does, we set the necessary HTTP headers for outputting the file and then call readfile() to output the file contents.

When we run this code, PHP will output the contents of data.txt to the browser (or trigger a download) and display the file’s data.

Besides outputting file contents to the browser, the readfile() function can also write the contents to another file. We simply change the first argument of readfile() to the target file path. Example code is shown below:

<?php
$sourceFile = 'data.txt'; // source file
$targetFile = 'output.txt'; // destination file

if (file_exists($sourceFile)) {
    readfile($sourceFile, $targetFile); // output contents to target file
} else {
    echo "File does not exist.";
}
?>

After running this code, PHP will write the contents of data.txt into output.txt .

By using readfile() we can conveniently output file contents to the browser or to other files; whether for file download or file copying, readfile() is a very practical function.

BackendPHPfile handlingdownloadreadfile
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.