Many times when we go to download software we click the vendor’s download link on their site and are immediately taken to a download page where it says “the download will start immediately, if not please click this link.” So how do we actually go about doing this?

There is a lot of cool things we can do with this as appose to directly linking to the file we want our customer/visitor to download. A couple bonuses:

We can mask the download link so the visitor doesn’t know where the actual file resides

We can keep statistics on the visitors that download our file

The first thing we need is a download confirmation page. Any HTML will do. The one thing we need though is a redirect to our download script. Add this into your HEAD tag:

This will attempt to load http://www.domain.com/download_file.php after 1 second. Make sure you change http://www.domain.com/download_file.php to the actual location of your download file. You can change the 1 to whatever you wish. Remember that the unit is in seconds.

The first thing we do is log the download into our database to keep track of the IP (can use this to find location), User Agent (can use to calculate browser stats), and the referrer string. I have it set to $_SESSION[“referer”] because I store any external referrer link in this so I can keep track of what site the visitor came to my site from. If you just want the last page they were on, use $_SERVER[“HTTP_REFERER”].

Now on to the download code. We set $file as the file path on our server we want to download relative our current download_file.php script. Now we check to make sure the file exists then output the headers to the browser that tell it were starting a file transfer. A couple notes about the headers:

file.zip can be changed to anything we want the download file to be named.

The Content-Type header is dependent of the type of file you are serving. See this list for the different MIME types