PHP Developer

My notes and thoughts about Linux, WordPress, PHP, WPML, Toolset and many more.

Best practices and Tips of using PHP readfile function – Developers Guide

If you need to read a file then stream it to the browser, then you need to use PHP readfile function. Common examples and applications includes using PHP to stream MP3 files, videos or even PDF files. This is a quick PHP developer guide on readfile, and this is based on the years of experience of using this function. By reading this guide, you will eliminate a lot of errors and bugs in your PHP application that depends on readfile. OK, lets get started.

Tip #1: Use absolute server path instead of using http

I have found that in some server configurations this will NOT work well:

But this work in ALL cases:

The only requirement is that you will need to know the absolute server path to the file you are going to read. You can read this tutorial on how get the absolue server path name of the file: https://www.php-developer.org/how-to-get-the-absolute-server-path-name-of-an-executing-script-in-php/

If you will use this:

You might noticed that the downloading script won’t work properly because it only downloads around 1KB despite that the file is around 10MB for example.

Tip #2: Define the content header type before calling READFILE

You need to define what type of content you need to stream before calling READFILE. This is to ensure that browsers will appropriate stream the correct content type. You can do this easily using header function in PHP. Supposing you are going to stream PDF using readfile:

In the above example, you have pre-defiend that you need to stream PDF. And that you need to force download it to your user (specified by the content disposition: attachment). Note that in some cases, you still cannot force download it. The solution is to use .htaccess. Put the download files inside a folder (e.g. downloads). Then put .htaccess inside that folder with the code:

It assumes you web host enables the header directive in Apache. Once the headers are well set, you can then stream it to your user using the readfile function.

Tip #3: To troubleshoot corrupt file using Readfile, open the corrupt file using text editor or in the browser

PHP requires that you will NOT output any HTML before calling any header function. Sometimes in a complicated script you might get lost with this principle and you forget this rule. As a result, there are HTML being outputted before the readfile function. This will result to corrupt file downloads. Steps when troubleshooting corrupt file downloads using Readfile PHP function:

1.) Create a text file (any content will do) then save it as text.txt then compress it as zip. The resulting filename is text.txt.zip.
2.) Upload this zip file to your root directory, get the absolute path of the file.
3.) Edit the script below to change it to your own path.

4.) Name the script as downloadtesting.php and upload it to the root directory of your website.
5.) Run downloadtest.php in the browser (e.g. http://www.thisisyourwebsite.com/downloadtesting.php), this will force download text.txt.zip. You might noticed you will get this error:

Warning: Cannot modify header information - headers already sent by (output started at /home/codexm/public_html/downloadtesting.php:6) in /home/codexm/public_html/downloadtesting.php on line 8

Warning: Cannot modify header information - headers already sent by (output started at /home/codexm/public_html/downloadtesting.php:6) in /home/codexm/public_html/downloadtesting.php on line 9

The problem is that the headers are already sent in line 8, but you will actually outputting these html blocks before the HEADER function thus resulting to file corruption:



This is my download page

The solution is to remove this HTML and put headers first, such as shown below:





This is my download page



Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">