Source: download.phps - download
<?php
//by daif alotaibi (daif.net) http://daif.net/script/download.phps
//This SQL will create the download table ,use PHPmyadmin to manage this table
/*
CREATE TABLE IF NOT EXISTS `download` (
`file_id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'file id',
`file_path` varchar(255) NOT NULL COMMENT 'file_path',
PRIMARY KEY (`file_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='download table';
*/
//config
$db['host'] = 'localhost'; //database host
$db['user'] = 'root'; //database user
$db['pass'] = ''; //database password
$db['name'] = 'test'; //database name
//connect to database
$link = mysql_connect($db['host'], $db['user'], $db['pass']);
//select database
mysql_select_db($db['name']);
//deal with request
if(isset($_GET['id']) && $_GET['id']>0) {
$id = (int) $_GET['id'];
$row = mysql_fetch_assoc(mysql_query('SELECT * FROM download WHERE file_id='.$id));
if(is_array($row) && file_exists($row['file_path'])){
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($row['file_path']));
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($row['file_path']));
ob_clean();
flush();
readfile($row['file_path']);
exit;
} else {
print 'File Not Found.';
//print file_get_contents('file_not_found.html'); //use this if you want make html page for error
}
} else {
print 'File Not Found.';
//print file_get_contents('file_not_found.html'); //use this if you want make html page for error
}
?>