Php Codeigniter强制下载文件

Php Codeigniter强制下载文件,php,codeigniter,Php,Codeigniter,通过阅读Codeigniter文档,我使用以下代码强制从我的服务器下载文件 function download($file_id){ $file = $this->uploadmodel->getById($file_id); //getting all the file details //for $file_id (all details are store

通过阅读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和头。我最终使用了我在这里找到的代码。使用以下功能,而不是强制下载。到目前为止,它对我很有效

    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();
    }
}

希望有帮助。

它也在为
.pdf
s工作。 检查文件路径-这可能是问题所在。
一、 也有这个问题,但当我更正文件路径时,它工作得非常好。
下面是我如何编写代码的:

if($src ==  "xyyx")
{
$pth    =   file_get_contents(base_url()."path/to/the/file.pdf");
$nme    =   "sample_file.pdf";
force_download($nme, $pth);     
}

试试这个,我认为这是解决这个问题的最佳方法

function download($file_id)
{
        $this->load->helper('file'); // Load file helper
        $file = $this->uploadmodel->getById($file_id); //Get file by id
        $data = read_file($file->full_path); // Use file helper to read the file's
        $name = $file->file_name;;

        force_download($name, $data);
}

不工作怎么办?不下载吗?无法在PDF阅读器中打开?错误?是的。。当我点击我创建的
下载/32
链接时,它只是打开链接,它是一个空白页。它是
download/$file\u id
…zip、.jpg、.txt、.gif都在工作,但没有。pdf检查服务器的错误日志或在应用程序文件夹中名为error\u log的文件中,您可能会在那里找到一些有用的信息。如果两者都不起作用,请尝试其他浏览器或尝试清除当前浏览器的缓存。当您尝试下载pdf时,是否可以打印$file的数组。可能这是旧版本的codeigniter的问题,但现在我使用的是codeigniter 2.2,pdf没有问题,RJ Anoop的回答很有用。@Carlos嵌入的URL不再存在,它是404。编辑:我在编辑中删掉了它。一开始不起作用。只是显示一个空白页。我不想添加“.pdf”扩展名,因为我无法打开流:HTTP请求失败!HTTP/1.1 403禁止这不是脚本的问题,而是您请求的资源的问题。web服务器正在返回“禁止”状态代码。