How to Download a File with PHP

PHP

You can do a lot of things with PHP scripts, including the automatically downloading of files that are stored on your server. In this tutorial, I will talk about how to download a file from your website using PHP.

You can place file download links on your web pages using plain simple HTML (the <a> link element), which is a nice way for allowing your visitors to download certain files from our server. Though it is easy to implement and it will be enough for most cases, there are times where you may need the ability of downloading files automatically or programmatically based on certain conditions and/or user interaction.

As a basic example, you may need to start the downloading of a file as soon as a page is opened, or you may need to add a timer to start the download. Besides, not directly linking to the downloadable files stored on your server and having them downloaded via a script is also a good practice to prevent hotlinkers. With the help of PHP, we can accomplish these things by creating a script to handle the file downloads.

As I explained in another post, you can also download files with JavaScript, but that will lack the server side scripting capability you may need when working with files and downloads. So, the following PHP file download script should help in cases where JavaScript is not the solution that you need.

How to Download Files with PHP

Obviously, we will need a file to be downloaded before starting with our download script. Simply create a text, image or video file (or any type of file) and put it on a directory on your website. In this example, we will be downloading a text file (myfile.txt) that is located in the files folder on our website.

/files/myfile.txt

Next, create a PHP file (e.g. download.php) which will contain the download script. You can name the file as you wish and place it where you want on your website. Just make sure to use the correct file path while downloading files.

In our script, we first specify the location (path to the file on your site) and the name of the file that will be downloaded, like the following:

<?php
$file = 'files/myfile.txt';
$filename = 'myfile.txt';
?>

We then specify the content-type header for the file to be downloaded:

header('content-type: text/plain');

We used text/plain content type (MIME type) since the file we will download is a plain text file. You can change that value depending on what type of file you want to download.

Then, we specify the filename and set the content-disposition header as an attachment, as in not a file to be displayed inside the browser window but a file to be downloaded like an attachment.

header('content-disposition: attachment; filename="'.$filename.'"');

Finally, we echo the file to be downloaded. Our final code will be like the following:

<?php
$file = 'files/myfile.txt';
$filename = 'myfile.txt';

header('content-type: text/plain');
header('content-disposition: attachment; filename="'.$filename.'"');
echo file_get_contents($file);
?>

Download an Image File with PHP

Similarly, you can use the following script to download an image file from your website:

<?php
$file = 'files/myimage.jpg';
$filename = 'myimage.jpg';

header('content-type: image/jpeg');
header('content-disposition: attachment; filename="'.$filename.'"');
echo file_get_contents($file);
?>

Note how we changed the content-type to image/jpeg for the JPG image file.

TIP: You can find a complete list of file MIME types here.

PHP File Download with Delay

If you need to add a timer to your download page, for example to display instructions or advertisements, you can use the sleep() function before handling the download as shown below:

<?php
// Display your download page content...

sleep(5);

$file = 'files/myfile.txt';
$filename = 'myfile.txt';

header('content-type: text/plain');
header('content-disposition: attachment; filename="'.$filename.'"');
echo file_get_contents($file);
?>

The above script will display the download page content, wait for 5 seconds and then start downloading the file.

As you noticed, our script will not make the file location publicly visible, therefore it will provide protection against hotlinkers (though you should also apply protection on your download directory).

f t g+ in