PHP Forums Archive

PHP file download

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

Posted by Olaf #

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 header to open also text files and other files which are opened by default inside the web browser. Use this PHP example code to download files from a password protected directory or if the files are stored above the public folder from your PHP website.

<?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>
?>

Script updates:

  • Added an example on how to use this script where the filename is added to the query string
  • Moved the disposition header into the switch because the attachment attribute is sometimes needed
  • Added the PHP function pathinfo() to retrieve some file information (the extension)
  • Added the PHP function stringconv() to handle also file extensions with upper caps.

Script Demo

The demo page demonstrates the PHP code examples for file upload and download and shows functions how to show directory content.

Comments / discussions

Posted by dushyanta #

Hey I am a newbie to php.
I am trying to export (download) excel files from my server, but I get garbage values inside the downloaded file. I tried is also with jpg files and here I get the same result. Any idea what I'm doing wrong?

Posted by Olaf #

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

Posted by natsort #

Hi, The script works perfect for me.
But how to redirect to a different website or page after the download has started?

When I add the HTML <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 like to provide some information to the user afterwards.

Posted by Olaf #

Hi,

if you click the link a 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 that changes the content on your downloads page. (please, don't post your questions about the client side code here, open a new topic 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.

Posted by MetalHippy #

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

Posted by Dunxmax #

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

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.

Posted by Olaf #

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/or the email script.