Wednesday, 6 June 2012

Download file using PHP


<?php
if($_REQUEST['Download'])
{
$file = 'filename.extension';
//If the file is not in the same location as that of this code file, then set the path in $file.

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
}
?>
<html>
<body>
<form name="form1" method="post" action="#">
<input type="submit" name="Download" value="Download">
</form>
</body>
</html>


If you have to give the file name as input, do it using a text box. 

No comments:

Post a Comment