Download Files in PHP

Avinash singh | 5:45 PM
<!--------------How to Download Files in PHP---------------!> 

<form action="#" method="get">
    <input type='submit' name='file' value='Avinash1568.jpg'>
</form>
<?php
if(isset($_GET['file']))
{   
//  get the directory location where downloadable files are listed
    $fullPath = 'downloads/'.$_GET['file'];  
    if (file_exists($fullPath)) {      
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
     header('Content-Length: ' . filesize($fullPath));
        header('Content-Disposition: attachment; filename="' . basename($fullPath) . '"');

        readfile($fullPath);
        exit();
    }

    // if file is not available send 404
    header( $_SERVER["SERVER_PROTOCOL"] . " 404 Not Found");
    include('../404.html');
    exit();
}

?>

Avinash Kumar Singh © 2013. All Rights Reserved | Powered by-Blogger