【问题标题】:downloading a file from server in codeigniter在codeigniter中从服务器下载文件
【发布时间】:2014-07-30 05:00:15
【问题描述】:

我正在尝试从我的服务器下载文件。

这是我的控制器代码。

public function download($id)
{
    $this->load->helper('url');
    $base_url = $this->config->item('base_url');
    $base_path = $this->config->item('base_path');
    $this->load->helper('download');

    $data = file_get_contents( $base_path.'/uploads/'.$id);
    $name = $id;
    force_download($name, $data);
}

但是,我收到一个错误:

file_get_contents(path/filename): failed to open stream: No such file or directory.

我该如何解决这个问题?谢谢

问题出在路径本身。实际上它不采用文件名,它们之间有空格。例如。现在可以使用文件名 xyz 但不能使用文件名 ex。试试xyz。我认为这是因为名称之间的空格。我该如何解决这个问题?

【问题讨论】:

  • 检查文件的路径是否正确或文件存在。尝试先回显路径
  • 检查 echo $base_path.'/uploads/'.$id;die; 的值
  • 是的,问题出在路径本身。实际上它没有在它们之间使用带有 spcae 的文件名。例如。现在可以使用文件名 xyz 但不能使用文件名 ex。试试xyz。我认为这是因为名称之间的空格。我该如何解决这个问题?

标签: php codeigniter


【解决方案1】:

改用 curl。 这可能是你的答案。

http://www.technologyworkshops.net/php/how-to-download-and-save-a-file-to-local-path-using-curl-t132.html

在你的助手中添加这个函数,它运行良好。

【讨论】:

    【解决方案2】:

    您可以使用魔术常量__ DIR__的绝对路径

    http://php.net/manual/en/language.constants.predefined.php

    $data = file_get_contents( __ DIR__.'/../uploads/'.$id);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-28
      • 2017-02-25
      • 1970-01-01
      • 1970-01-01
      • 2015-08-28
      相关资源
      最近更新 更多