我希望用户能够下载我服务器上的一些文件,但是当我尝试下载文件时显示如下错误。
public function saveattachment(){
$fn=$this->input->get_post('fn');
$userid=$this->input->get_post('autherid');
$file_url = realpath("./private").$userid."/".$fn;
$file_name =basename($file_url);
$ext = explode(".", $file_name);
switch($ext[sizeof($ext)-1])
{
case 'jar': $mime = "application/java-archive"; break;
case 'zip': $mime = "application/zip"; break;
case 'jpeg': $mime = "image/jpeg"; break;
case 'jpg': $mime = "image/jpg"; break;
case 'jad': $mime = "text/vnd.sun.j2me.app-descriptor"; break;
case "gif": $mime = "image/gif"; break;
case "png": $mime = "image/png"; break;
case "pdf": $mime = "application/pdf"; break;
case "txt": $mime = "text/plain"; break;
default: $mime = "application/force-download";
}
header('Content-Description: File Transfer');
header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename='.basename($file_name));
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_url));
ob_clean();
flush();
echo $file_url;
}
文件已下载但未打开。
答案 0 :(得分:0)
header(), 示例代码
<?php
header("Content-type:application/pdf");
// It will be called file_name_for_download.pdf
header("Content-Disposition:attachment;filename='file_name_for_download.pdf'");
// The PDF source is file_for_read.pdf
readfile("file_for_read.pdf");
?>
在您的情况下使用基本代码并确保其正常工作,然后根据要求添加其他代码
答案 1 :(得分:0)
将echo $file_url;
替换为readfile($file_url);
答案 2 :(得分:0)
我明白了。我编码为header('Content-Length: ' . filesize($file_url));
,我将其替换为header('Content-Length: ' . filesize($file_name));
。现在它的工作。谢谢你的时间来帮助我。