[英]How to force download zip file in codeigniter
我正在尝试使用 codeigniter 创建一个简单的数字商店 web 应用程序。 我想使用强制下载帮助程序 function 以便用户不会知道文件的真正 url。 我尝试按照 codeignter 中的文档进行操作 - 它可以工作,但文件已损坏。
这是我的代码
//* Get the website name
$site = $this->Settings_model->getApllicationInfo();
$sitename = $site->set_site_name;
//* Prepare file for downloading
$filename = $sitename.'-'.$item_info->item_id.'-'.$item_info->item_name;
$locate = './static/files/zips/'.$file;
force_download($locate, $filename);
它下载了文件,但它坏了,请帮助我或给我任何其他我可以使用的建议。
使用下面的 function 下载文件
function auto_download($path, $name) {
if(is_file($path)) {
if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }
$this->load->helper('file');
$mime = get_mime_by_extension($path);
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 ($path)).' GMT');
header('Cache-Control: private',false);
header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="'.basename($name).'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($path));
header('Connection: close');
readfile($path);
exit();
}
}
我希望您在为force_download()
的第一个参数提供的文件名上使用文件扩展名。 因为Codeigniter
使用MIME-type
。 我希望它对你有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.