php 无法使用干预库将图像数据写入 Laravel 中的路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25497694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
can't write image data to path in laravel using the Intervention library
提问by user3838153
I am using Laravel 4.2 with the Intervention library,and I am not sure why I am getting this problem when everything else seems to be right. here is the code:
我正在将 Laravel 4.2 与 Intervention 库一起使用,但当其他一切似乎都正确时,我不确定为什么会出现此问题。这是代码:
Route::post('test',function()
{
// check if files was sent
if(Input::hasFile('file'))
{
$image = Input::file('file');
$filename = date('Y-m-d-H:i:s').'-'.$image->getClientOriginalName();
$path = public_path('images/cars/'.$filename);
Image::make($image->getRealPath())->resize(468,249)->save($path);
} else {
return Response::json(array('test'=>false));
}
});
The error I am receiving is:
我收到的错误是:
Intervention \ Image \ Exception \ NotWritableException Can't write image data to path (C:\xampp\htdocs\jacars_project\public/images/cars/2014-08-26-03:41:39-icon_2034.png).
Intervention \ Image \ Exception \ NotWritableException 无法将图像数据写入路径 (C:\xampp\htdocs\jacars_project\public/images/cars/2014-08-26-03:41:39-icon_2034.png)。
Thanks in advance in helping me solve the problem
提前感谢帮助我解决问题
回答by ako
you can check for directory existence , if there is no such directory then create it :
您可以检查目录是否存在,如果没有这样的目录,则创建它:
if (!file_exists($save_path)) {
mkdir($save_path, 666, true);
}
回答by The Alpha
Just change this:
只需改变这个:
$path = public_path('images/cars/'.$filename);
To this:
对此:
$path = 'images/cars/' . $filename;
Also, make sure that, the target path/location
is writable, has write permission.
另外,请确保目标path/location
是可写的,具有写权限。
回答by Adrian Enriquez
By creating the directory first where I will put my image file solves the problem for me.
通过首先创建我将放置图像文件的目录为我解决了问题。
Ex: creating the public/images/cars/
directories first. Then you can now save the images there.
例如:public/images/cars/
首先创建目录。然后您现在可以将图像保存在那里。
PS: But if you want to create folders dynamically, I'm not sure how to do that.
PS:但是如果你想动态创建文件夹,我不知道该怎么做。
回答by 27 13
if you are using Ubuntu -> sudo chmod -R 777 public
如果您使用的是 Ubuntu -> sudo chmod -R 777 public