I have an example here using PHP to push the file to the browser. To trigger the download of the file, we will use HTML header statements. In PHP, this is possible using header function in passing raw HTML headers to the browser. In this example we will pass the filename in the url for instance - www.domainname.com/download.php?file=filename.zip. Of course, you can customize the codes according to the functionality of your website.
<?php
//path directory of the file
$dir="downloads/";
//cehck if there is a file passed
if (isset($_REQUEST["file"])) {
//concatinate the directory and the filename
$file=$dir.$_REQUEST["file"];
//set HTML headers that must be sent to the browser.
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, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
//read the file
readfile($file);
exit;
}
else {
//prompt if no file selected
echo "No file selected";
}
?>
Click here for the php header manual
No comments:
Post a Comment