通过阅读Codeigniter文档,我使用以下代码强制从服务器下载文件。
function download($file_id){
$file = $this->uploadmodel->getById($file_id); //getting all the file details
//for $file_id (all details are stored in DB)
$data = file_get_contents($file->full_path); // Read the file's contents
$name = $file->file_name;;
force_download($name, $data);
}
该代码是用于图像的工作文件,但是与PDF文件一起使用时,它不起作用。我尚未针对所有文件扩展名进行过测试,但是由于它不适用于PDF,因此可能不适用于其他各种文件类型。
有什么办法吗?
参考方案
我也遇到过类似的问题。我认为问题出在某些MIME和发送到浏览器的标头中。我最终使用了在http://taggedzi.com/articles/display/forcing-downloads-through-codeigniter中找到的代码。使用下面的函数代替force_download。到目前为止,它对我有用。
function _push_file($path, $name)
{
// make sure it's a file before doing anything!
if(is_file($path))
{
// required for IE
if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }
// get the file mime type using the file extension
$this->load->helper('file');
$mime = get_mime_by_extension($path);
// Build the headers to push out the file properly.
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 ($path)).' GMT');
header('Cache-Control: private',false);
header('Content-Type: '.$mime); // Add the mime type from Code igniter.
header('Content-Disposition: attachment; filename="'.basename($name).'"'); // Add the file name
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($path)); // provide file size
header('Connection: close');
readfile($path); // push it out
exit();
}
}
希望能帮助到你。
CodeIgniter ODBC问题 - php好吧,我已经构建了一个用于MSSQL的应用程序,最初以这种方式运行它,我购买了一台新计算机,出于某种奇怪的原因,它不允许我通过MSSQL连接。因此,我设置了ODBC。它连接良好,但似乎讨厌活动记录。我将不得不重写所有查询吗?还是我想念的东西。我收到这样的错误。A Database Error Occurred Error Number: 37000 [Mic…
如何在PHP中检查数组是否为空(Codeigniter) - php这是我的代码示例:public function show($imei,$start_time,$end_time,$dateto) { $from_time = str_replace('-','/',$start_time); $fromi=strtotime($from_time . ' ' …
交叉加入codeigniter? - php我正在尝试$this->db->join('tableTwo as b','','CROSS'); $result = $this->db->get('tableOne as a')->result(); 有解决办法吗? 参考方案 Codeignit…
在Codeigniter中以数组形式获取查询结果 - php我有一个查询,如何获取结果作为数组? php大神给出的解决方案 像这样:$rs = $this->db->query(...); $array = $rs->result_array(); https://www.codeigniter.com/user_guide/database/results.html
codeigniter 2.0.3-致命错误 - php我是Codeigniter的新手,尝试了其中一个教程的课程,但抛出以下错误:Class 'Controller' not found in C:\xampp\htdocs\CodeIgniter\application\controllers\email.php on line 3 我的代码:<?php class Email ex…