PHP Forums Archive

PHP file download

Tags: php, apache, file, cache control, protected directory

Olaf posted on 2009-09-17 05:25:05 #

This is my favorite download-script, it works with files bigger than 10 MB on Apache 1.3 (or higher). I added the cache control to open also text files and others (without saving). Use this example code to download files from a protected directory for example.

<?php

// place this code inside a php file and call it f.e. "download.php"
$path = $_SERVER['DOCUMENT_ROOT']."/path2file/"; // change the path to fit your websites document structure
$fullPath = $path.$_GET['download_file'];

if ($fd = fopen ($fullPath, "r")) {
    $fsize = filesize($fullPath);
    $path_parts = pathinfo($fullPath);
    $ext = strtolower($path_parts["extension"]);
    switch ($ext) {
        case "pdf":
        header("Content-type: application/pdf"); // add here more headers for diff. extensions
        header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
        break;
        default;
        header("Content-type: application/octet-stream");
        header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
    }
    header("Content-length: $fsize");
    header("Cache-control: private"); //use this to open files directly
    while(!feof($fd)) {
        $buffer = fread($fd, 2048);
        echo $buffer;
    }
}
fclose ($fd);
exit;
// example: place this kind of link into the document where the file download is offered:
// <a href="download.php?download_file=some_file.pdf">Download here</a>
?>


Updates:

Added an example how to use this script with the filename from a query string
Mved the disposition header into the switch because the attachment attribute is sometimes needed
Using the function pathinfo() to get the file information (extension) now
Added the function stringconv() to handle also extensions with caps.

Demo:
The demo page demonstrate the functions file upload / download and handle directories

Comments / discussions

dushyanta posted on 2010-01-21 20:34:48 #

Hey I am a newbie to php.
I am trying to export (download) excel file from my server but i get garbage values in the downloaded file. I tried with jpg with the same result. Any idea what i might be doing incorrectly.

Olaf posted on 2010-01-21 20:59:16 #

You're using the code from that page?
- check the paths (most important)
- is the file size from the download similar to the original?

natsort posted on 2010-01-25 13:59:29 #

Hi, The script works perfect for me.
But how about loading a new website after download has started?

When I add <a rel="nofollow" href="download.php?download_file=some_file.pdf">Download here</a>

to my main page - it just downloads the file. But I want to give some information to users afterwords??

Olaf posted on 2010-01-25 15:02:14 #

Hi,

if you click the link an second script is called and the output is send to the browser. You can't add any information here. It's possible to create some message on the page where the download link is placed. You need to use some client side code on that page:

<a href="file.pdf" onclick="someJSfunction()">Download here</a>

The function someJSfunction() should be some Javascript code to change the content on your downloads page. (please, don't post your questions about the client side code here open a new thread instead)

On other sites you find the information before the download starts, most of them use a meta refresh tag to start the download after X seconds.

MetalHippy posted on 2010-02-26 15:25:11 #

Just registered to say what a great script this is - I use a host which prevents direct file downloads and this is just what I was looking for.

Keep up the good work, and thanks from me! :D

Dunxmax posted on 2010-03-18 13:02:50 #

Thank you for this script - it does exactly what it says on the tin.

I am not a programmer, but am trying to add a bit more functionality when the user hits the download button. What I would like to be able to do is to send an auto email at the same time.

Up until I found your script I had managed to get an auto email to work which then linked back through an email to being able to download a file.

I want to find out if it is possible to do both at the same time and how to do it. I tried putting both scripts in the same php file, but this didn't work.

If anyone can point me in the right direction that would be great.

D

Olaf posted on 2010-03-18 13:08:46 #

Hi Dunxmax,

sure this is a great function but is not really related to this code snippet ;)

Please start a new topic with the code you already have. First of all you need a form and an email script.