<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<?php
$file = $_GET['file'];
if (!is_file($file)) {
print('<script language="javascript" type="text/javascript">');
print('alert("对不起,文件找不到!");');
print('</script>');
} else {
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Type: application/force-download');
header('Content-Type: application/octet-stream');
header('Content-Type: application/download');
if ($_GET['mime_type']) {
header('Content-Type: ' . $_GET['mime_type']);
}
if (eregi('MSIE', $_SERVER['HTTP_USER_AGENT']) && eregi('5.5', $_SERVER['HTTP_USER_AGENT'])) {
header('Content-Disposition: filename=' . basename($_GET['filename']) );
} else {
header('Content-Disposition: attachment; filename=' . basename($_GET['filename']) );
}
header('Content-Transfer-Encoding: binary');
header("Content-Length: " . filesize($file));
readfile($file);
}
?>
通过URL传入以下参数:
$_GET['file']:文件在服务器上的路径。
$_GET['filename']:用户下载到本地后的文件名。
$_GET['mime_type']:文件的MIME类型,可以省略。
以上代码可是实现任何文件类型的下载。
使用方法链接:
<a href="'.$website.'services/download-file.php?file=../cnimage/thinksnet/uploads/download/'.$rs[$i]['Down_File'].'&filename='.$rs[$i]['Down_File'].'" target=_blank></a>