通过执行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.到目前为止我已经为我工作了.
原文链接:https://www.f2er.com/php/132199.htmlfunction _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(); }
希望它有帮助.