How to Download an External File with PHP

PHP
f t g+ in

It is possible to download a file that is hosted on another website/server to your server or to the user's computer with the help of a simple PHP script. In this tutorial, I will demonstrate how to download an external file using PHP.

PHP is a highly versatile server-side programming language that you can use in your web development work to accomplish a wide range of website or web application related tasks including the downloading of files.

You may need to include a download feature on your site or application for a number of reasons. For example, if you are selling digital download products, offering video downloads or sharing downloadable files with your visitors for free, or if your web application allows its users to create and download PDF reports, CSV files (e.g. office related files), ZIP archives or image or file attachments, you will certainly need download capability on your project.

With PHP, you can download and let your users download files not only from your own website but also from other websites which you have no control of (i.e. third party websites hosted on other domains or servers). While this is an extremely handy (necessary in some cases) feature to have, it should be used only within the terms of the target website(s) and it is never to be used for illegal or non user-friendly activities such as forced downloads, malicious file downloads etc.

Also Check:
How to Add Chrome Userstyles
12 Web Developer Extensions for Chrome
JavaScript Redirect Tutorial
5 Notepad++ Essential Plugins to Download

I talked about topics such as creating a ZIP archive with PHP, downloading a file with PHP and downloading a file with JavaScript in other tutorials. Now, I will continue with downloading an external file, a file that resides on another website, with PHP.

Allow the User to Download an External File with PHP

Downloading a file with PHP can be accomplished in two ways. The first one is to get the file contents and prepare it for download, and let the user initiate the download process by either opening a page or clicking on a button such as an HTML download link. For this purpose, we will create a PHP file, e.g. download.php.

We will use the example domain (example.com) and a PDF file (my-file.pdf) for demonstration purposes. Our first step is to assign the file URL to a variable and assign a file name to the file that will be downloaded, like the following:

<?php
$url = "http://www.example.com/my-file.pdf";
$file_name = "my-file.pdf";
?>

You can name your variables as you wish, but make sure you specify the URL of the file correctly. The actual file name and the downloaded file name doesn't need to be the same.

Secondly, we specify the correct headers for the PDF file so that it can be parsed correctly.

<?php
$url = "http://www.example.com/my-file.pdf";
$file_name = "my-file.pdf";

header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename='".$file_name."'");
?>

We used the header() function two times, first to specify the MIME type for the PDF file (application/pdf) and second to specify the content disposition method (attachment) and the file name. Note that you will need to use the correct MIME type for different files. Pay attention to the correct use of the single and double quotes. You may also check this list of file MIME types to find which one you will need for your file downloads.

The last thing we need is to get the file content using the file_get_contents() and echo it, so that the file will be ready for download when this script is run.

<?php
$url = "http://www.example.com/my-file.pdf";
$file_name = "my-file.pdf";

header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename='".$file_name."'");

echo file_get_contents($url);
?>

Place the above code into your download.php file and link to it where you will offer the download of the file, i.e. after email submission. When the user clicks that link and the download script is run, depending on the user's browser settings, a download will automatically start or ask for user's confirmation to save the file.

Download External Files to Your Server with PHP

The second way for downloading a file from an external server using PHP is to directly download and save the file to the server, with or without user interaction.

We start with defining the target file URL and download file destination. The download file destination is the location where the downloaded file will be saved on your server.

<?php
$url = "http://www.example.com/my-file.pdf";
$file_destination = "/files/my-file.pdf";
?>

and continue with reading the file content with file_get_contents() function and then writing it to a file on the server at the specified destination using the file_put_contents() function.

<?php
$url = "http://www.example.com/my-file.pdf";
$file_destination = "/files/my-file.pdf";

$file_content = file_get_contents($url);
file_put_contents($file_destination, $file_content);
?>

Once this script is run, the target PDF file will be downloaded to your server and saved at the specified location.

Also note that your web hosting server (or localhost if you're working on a local setup) should have enabled the use of the file_get_contents() and file_put_contents() functions in order for these download scripts to work.

This concludes our tutorial about how to download external files with PHP and hopefully it will be helpful in your development projects where you will need to implement download functionality.

Was this post helpful?

If you found this post to be helpful, please share it! Thank you :)

f t g+ in