我正在尝试从我的服务器下载文件。
这是我的控制器代码。
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。我认为这是因为名字之间有空格。我该如何解决这个问题?
您可以使用魔术常量 __ DIR__ 的绝对路径
http://php.net/manual/en/language.constants.predefined.php
$data = file_get_contents( __ DIR__.'/../uploads/'.$id);
使用curl代替。 这可能就是你的答案。
在你的助手中添加该功能,效果很好。
此处链接已损坏 PASTIN 代码
与您分享一段代码,该代码将使用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;
}