我是初学者的php程序员。我已经编写了下载任何类型文件的代码。
当我点击下载链接时,它会转到download.php文件。我在本地服务器上工作,但不在服务器上工作。
我的代码是:
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream'); //application/force-download
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();
readfile($file);
exit();
我的代码是错误的还是服务器需要一些设置?
这是经过在线代码测试的,运行良好。你可以试试这个
$folder_name = $_GET['fol_name'];
$file_directory = "../img/files/$folder_name"; //Name of the directory where all the sub directories and files exists
$file = $_GET['file_name']; //Get the file from URL variable
$file_array = explode('/', $file); //Try to seperate the folders and filename from the path
$file_array_count = count($file_array); //Count the result
$filename = $file_array[$file_array_count-1]; //Trace the filename
$file_path = dirname(__FILE__).'/'.$file_directory.'/'.$file; //Set the file path w.r.t the download.php... It may be different for u
if(file_exists($file_path)) {
header("Content-disposition: attachment; filename={$filename}"); //Tell the filename to the browser
header('Content-type: application/octet-stream'); //Stream as a binary file! So it would force browser to download
readfile($file_path); //Read and stream the file
}
else {
echo "Sorry, the file does not exist!";
}
谢谢你!!
使用我编写的代码。这个问题得到了解决。若有人有同样的问题。请试试这个代码。对我来说效果很好。
$file_name ='../img/files'.DS.$_GET['file'];
if(is_file($file_name)) {
if(ini_get('zlib.output_compression')) {
ini_set('zlib.output_compression', 'ON');
}
switch(strtolower(substr(strrchr($file_name, '.'), 1))) {
case 'pdf': $mime = 'application/pdf'; break; // pdf files
case 'zip': $mime = 'application/zip'; break; // zip files
case 'jpeg': $mime = 'image/jpeg'; break;// images jpeg
case 'jpg': $mime = 'image/jpg'; break;
case 'mp3': $mime = 'audio/mpeg'; break; // audio mp3 formats
case 'doc': $mime = 'application/msword'; break; // ms word
case 'avi': $mime = 'video/x-msvideo'; break; // video avi format
case 'txt': $mime = 'text/plain'; break; // text files
case 'xls': $mime = 'application/vnd.ms-excel'; break; // ms excel
default: $mime = 'application/force-download';
}
header('Content-Type:application/force-download');
header('Pragma: public'); // required
header('Expires: 0'); // no cache
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)); // provide file size
header('Connection: close');
readfile($file_name);
exit();
}
谢谢你!!!
我最近遇到了这个问题,发现这是由ob_clean()引起的;和flush();这些导致程序下载垃圾。
我尝试了各种组合来刷新缓冲区,唯一在托管服务器上工作的是ob_end_clean();打印$object->body;
它可能也适用于echo,但我没有尝试
使用此
public function loadfile($fl)
{
$mime = 'application/force-download';
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private',false);
header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="'.basename($fl).'"');
header('Content-Transfer-Encoding: binary');
header('Connection: close');
readfile($fl); // push it out
exit();
}