简体   繁体   English

无法使用干预库将图像数据写入 laravel 中的路径

[英]can't write image data to path in laravel using the Intervention library

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.我将 Laravel 4.2 与 Intervention 库一起使用,我不确定为什么在其他一切似乎都正确时会遇到这个问题。 here is the code:这是代码:

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在此先感谢您帮助我解决问题

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);
        }

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 creating the directory first where I will put my image file solves the problem for me.通过首先创建我将放置图像文件的目录为我解决了这个问题。

Ex: creating the public/images/cars/ directories first.例如:首先创建public/images/cars/目录。 Then you can now save the images there.然后您现在可以将图像保存在那里。

PS: But if you want to create folders dynamically, I'm not sure how to do that. PS:但如果你想动态创建文件夹,我不知道该怎么做。

After so many hours, I found the solution for Centos 8 (but it works for others too).经过这么多小时,我找到了 Centos 8 的解决方案(但它也适用于其他人)。 We need to assign the necessary permissions to the folder that will save the images to upload.我们需要为将保存要上传的图像的文件夹分配必要的权限。 And using the "chmod" command is not enough, you need to use而使用“chmod”命令是不够的,还需要使用

httpd_sys_content_t httpd_sys_content_t

and

httpd_sys_rw_content_t httpd_sys_rw_content_t

Ex: From the command line we enter the folder of our Laravel project, we enter the "/public" folder and execute the following commands to give permissions to the "/images" folder:例如:从命令行我们进入我们的 Laravel 项目的文件夹,我们进入“/public”文件夹并执行以下命令来授予“/images”文件夹的权限:

chown –R apache: images
chmod –R 755 images
systemctl restart httpd
chcon -R -t httpd_sys_content_t images
chcon -R -t httpd_sys_rw_content_t images
systemctl restart httpd

Note: ("chown -R apache: images" replaces in other Linux versions its equivalent "chown -R www-data: images").注意:( “chown -R apache: images”在其他 Linux 版本中替换其等效的“chown -R www-data:images”)。

If you haven't created your path's folder, you will get that error.如果您尚未创建路径的文件夹,您将收到该错误。 You need to create first it.您需要先创建它。

如果您使用的是 Ubuntu -> sudo chmod -R 777 public

就我而言,它没有文件夹的权限, chmod 和 755 完成了这项工作。

chmod -R 755

If you are on shared hosting, don't use public_path() helper function to point to the uploads location.如果您在共享主机上,请不要使用 public_path() 辅助函数指向上传位置。 simple put the location and you are good to go:简单地放置位置,您就可以开始了:

change public_path(UPLOAD_PATH)更改 public_path(UPLOAD_PATH)

to only UPLOAD_PATH只到 UPLOAD_PATH

问题是权限问题,只要进入你的目录,给它读写权限,你的问题就解决了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM