【问题标题】:File download error when file has no content or size is 0文件没有内容或大小为 0 时文件下载错误
【发布时间】:2013-06-15 10:45:35
【问题描述】:

如果文件大小为 0 字节或里面没有任何内容,我无法下载任何文件(文本、文档或任何其他扩展名)。这是我使用的代码如下。

$filename = $this->input->get('filename');              
$fileoriginalname = $this->input->get('fileorgname');

$this->load->helper('download');
$Path = 'files/'.$filename;
$data = file_get_contents($Path);

force_download($fileoriginalname, $data);

谁能帮我解决。如果文件里面有任何内容,上面的代码就可以正常工作,但如果没有内容就停止工作,所以我很困惑,有点沮丧。

【问题讨论】:

  • 你说的不能是什么意思?是否有某种错误? file_get_contents 是否返回 false?
  • 没有错误,但页面为空白,无法下载文件。而file_get_contents 正在返回 true。

标签: php file codeigniter download


【解决方案1】:

我也遇到了这个问题,但不知怎的,我得到了File Size Zero的帮助

您可以使用以下代码,看看是否有帮助。

不要使用force_download 试试这个

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');     // 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);  
    header('Content-Disposition: attachment; filename="'.$fileoriginalname.'"');  // Add the file name
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: '.filesize($Path)); 
    header('Connection: close');
    readfile($Path); 
    exit();

【讨论】:

    【解决方案2】:

    我在这里猜测,看不到其余代码。

    我假设当您尝试下载 0bytes 的文件时,发生了错误

    所以我可能会写一些类似的东西

    if(!force_download($fileoriginalname, $data))
    {
        \\ the file download failed. handle this error
    }
    

    【讨论】:

    • 是的,我这样做了,我收到了错误消息,谢谢,但这不是我的问题,而是如何解决它并下载一个 0 字节的文件。
    猜你喜欢
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 2016-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多