在codeigniter中从服务器下载文件

7rfyedvj  于 11个月前  发布在  其他
关注(0)|答案(2)|浏览(85)

我想从服务器上下载一个文件。
下面是我的控制器代码。

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.我认为这是因为空间之间的名称.我怎么能解决这个问题?

nzkunb0c

nzkunb0c1#

您可以使用绝对路径到魔术常数__
http://php.net/manual/en/language.constants.predefined.php

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

字符串

woobm2wo

woobm2wo2#

用curl代替。这可能是你的答案。
http://www.technologyworkshops.net/php/how-to-download-and-save-a-file-to-local-path-using-curl-t132.html
在你的助手中添加这个函数,它工作得很好。
由于链接已断开,请在此处输入密码
与您分享一段代码,它将使用curl将文件保存到您的本地目录。

$getFile = 'https://url to file/file_name.csv';
$getParams  = array ();

$path = '/var/save/to/local/path/file_name.csv';
$fp = fopen($path, 'w');
$ch = curl_init($getFile);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies');
$data = curl_exec($ch);

if(fwrite($fp,$data))
{
    return true;
}
else
{
    return false;
}

字符串

相关问题