当用户单击链接时,我正在尝试下载文件。该文件位于其他网站上,我正在传递文件的 url,例如 xyz.com/filename.mp3。
echo "<a href=/direct_download.php?file=$link[$j]><img border='0' src='images/dl.gif' ></a>";
这是我的下载文件脚本。
<?php
$file_name = $_GET['file'];
//$files = rawurldecode($file_name);
echo $file_name;
if(is_file($file_name)) {
echo $file_name;
if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }
switch(strtolower(substr(strrchr($file_name, '.'), 1))) {
case 'pdf': $mime = 'application/pdf'; break;
case 'zip': $mime = 'application/zip'; break;
case 'jpeg':
case 'jpg': $mime = 'image/jpg'; break;
case 'mp3': $mime = 'audio/mpeg';break;
default: $mime = 'application/force-download';
}
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($file_name)).' GMT');
header('Cache-Control: private',false);
header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($file_name));
header('Connection: close');
readfile($file_name);
exit();
}
?>