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
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.
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.
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.
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").
If you haven't created your path's folder, you will get that error. You need to create first it.
if you are using Ubuntu -> sudo chmod -R 777 public
In my case, it was not having permission on the folder, chmod with 755 did the job.
chmod -R 755
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With