File download script for websites

2 2,110

Sometimes bloggers/webmasters want to share something with their users using download and therefore they put the link of the resource on their website so that users/visitors can download the resource. Mostly they point the link to the resource itself and therefore web browser tries to open it in itself.

Here is the example of the above explanation: Bitnol.

When you click on the link it will open the image in the browser. So, now the user has to save it using save option or ‘CTRL + S’ in Windows OS.

But this is not the correct way for providing your user to download any file. Yes, it is good for viewing images in the browser but still, the user has to save it.

Now the solution is below small PHP script which will handle all your downloads:

<?php
$file = $_GET['file'];
download_file($file);
function download_file($fullPath)
{
    
    // Must be fresh start
    if (headers_sent())
        die('Headers Sent');
    
    // Required for some browsers
    if (ini_get('zlib.output_compression'))
        ini_set('zlib.output_compression', 'Off');
    
    // File Exists?
    if (file_exists($fullPath)) {
        
        // Parse Info / Get Extension
        $fsize      = filesize($fullPath);
        $path_parts = pathinfo($fullPath);
        $ext        = strtolower($path_parts["extension"]);
        
        // Determine Content Type
        switch ($ext) {
            case "pdf":
                $ctype = "application/pdf";
                break;
            case "exe":
                $ctype = "application/octet-stream";
                break;
            case "zip":
                $ctype = "application/zip";
                break;
            case "doc":
                $ctype = "application/msword";
                break;
            case "xls":
                $ctype = "application/vnd.ms-excel";
                break;
            case "ppt":
                $ctype = "application/vnd.ms-powerpoint";
                break;
            case "gif":
                $ctype = "image/gif";
                break;
            case "png":
                $ctype = "image/png";
                break;
            case "jpeg":
            case "jpg":
                $ctype = "image/jpg";
                break;
            default:
                die('Forbidden !!!');
        }
        
        header("Pragma: public"); // required
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: private", false); // required for certain browsers
        header("Content-Type: $ctype");
        header("Content-Disposition: attachment; filename=\"" . basename($fullPath) . "\";");
        header("Content-Transfer-Encoding: binary");
        header("Content-Length: " . $fsize);
        ob_clean();
        flush();
        readfile($fullPath);
    } else {
        die('File Not Found');
    }
}
?>

Copy and save the above PHP script as ‘download.php’ (whatever name you want) and put it in your server. Now to download any file call it as follows:

http://YOURDOMAIN/download.php?file=PATH 
Where YOURDOMAIN = your actual domain name
and PATH = path to the file to download 

Now your download system is ready. You can add more security using session ids like creating a session token for each download so that no one can steal your bandwidth.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More