CodeIgniter强制下载不工作

tkclm6bt  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(112)

CodeIgniter force_download不工作
我的代码:

if(!empty($articlefile)){
  if(file_exists('./media/journals/xml/'.$articlefile)){
    $this->load->helper('download');
    $articlefile = str_replace(" ", "%20", $articlefile); 
    $path=file_get_contents(getBaseUrl().'media/journals/xml/'.$articlefile);
    force_download($articlefile,$path);
  }else{
    redirect(getBaseUrl());
    exit();
  }
}

同样的代码以前工作正常,今天这个代码不工作我不知道为什么?我使用的PHP 7.4版本,但仍然得到错误。

csbfibhn

csbfibhn1#

通过查看您的代码,我想知道在发出force_download调用之前$path变量包含什么。例如,如果存在$path = null的错误,那么force_download将失败,因为$articleFile不是完整路径。从您的代码中可以看出,文章文件位于本地文件系统上,我对您在file_get_contents调用中使用getBaseUrl提出了质疑。
我还将尝试一个基本的示例,看看您的路由和控制结构是否按预期工作。
使用手册中force_download页面的示例和您的代码,我可能会尝试:

if(!empty($articlefile)){
if(file_exists('./media/journals/xml/'.$articlefile)){
$this->load->helper('download');
//$articlefile = str_replace(" ", "%20", $articlefile); 
//$path=file_get_contents(getBaseUrl().'media/journals/xml/'.$articlefile);
//force_download($articlefile,$path);

$data = 'Here is some text!';
$name = 'mytext.txt';
force_download($name, $data);
}
else{
redirect(getBaseUrl());
exit();
}
}

如果这样做有效,您可以分别尝试名称和数据变量,以缩小未按预期执行的变量范围。

相关问题