17
votes

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:

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).

Thanks in advance in helping me solve the problem

7

7 Answers

27
votes

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

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.

2
votes

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.

PS: But if you want to create folders dynamically, I'm not sure how to do that.

2
votes

After so many hours, I found the solution for Centos 8 (but it works for others too). 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

httpd_sys_content_t

and

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:

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").

1
votes

if you are using Ubuntu -> sudo chmod -R 777 public

0
votes

If you are on shared hosting, don't use public_path() helper function to point to the uploads location. simple put the location and you are good to go:

change public_path(UPLOAD_PATH)

to only UPLOAD_PATH

0
votes

In my case, it was not having permission on the folder, chmod with 755 did the job.

chmod -R 755