我试图使用Laravel文件系统获取外部文件,但我一直收到File does not exist at path
异常
这是我的代码
File::get('http://lorempixel.com/272/172');
Laravel函数被称为
public function get($path, $lock = false)
{
if ($this->isFile($path)) {
return $lock ? $this->sharedGet($path) : file_get_contents($path);
}
throw new FileNotFoundException("File does not exist at path {$path}");
}
我已经在我的服务器上测试了file_get_contents('http://lorempixel.com/272/172')
,它运行良好。
这很奇怪,如果在if语句中执行dd($path)
,我会得到
"/home/vagrant/Code/test/storage/framework/sessions/fdb554540ba30a38464950b36908fa20e0ee94cc"
返回
如果我在If语句外执行dd($path)
,我会得到返回的http://lorempixel.com/272/172
我被难住了
File::get
仅适用于本地文件,在4.1之前的laravel版本中,您可以使用File::getRemote
:
$content = File::getRemote("http://lorempixel.com/272/172");
但在laravel 4.1中删除了它。根据拉拉威尔的创造者@taylortwell的说法,这是不安全的。它所做的只是调用file_get_contents
,所以只要用file_get_contents
替换它,你就应该很好了。