Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

laravel

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

like image 714
user3838153 Avatar asked Aug 26 '14 03:08

user3838153


People also ask

What is intervention image in laravel?

Intervention Image is a PHP image handling and manipulation library providing an easier and expressive way to create, edit, and compose images. The package includes ServiceProviders and Facades for easy Laravel integration.


7 Answers

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);
        }
like image 139
ako Avatar answered Sep 25 '22 10:09

ako


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.

like image 27
The Alpha Avatar answered Sep 21 '22 10:09

The Alpha


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.

like image 30
Adrian Enriquez Avatar answered Sep 25 '22 10:09

Adrian Enriquez


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

like image 23
GastonChiquillo Avatar answered Sep 21 '22 10:09

GastonChiquillo


If you haven't created your path's folder, you will get that error. You need to create first it.

like image 28
Enver Avatar answered Sep 23 '22 10:09

Enver


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

like image 33
Lawson Avatar answered Sep 22 '22 10:09

Lawson


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

chmod -R 755
like image 41
Masood Ibraheem Avatar answered Sep 22 '22 10:09

Masood Ibraheem