Reputation: 291
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
Upvotes: 27
Views: 107686
Reputation: 302
if ($request->hasFile('image')) {
$ds = DIRECTORY_SEPARATOR;
$image = $request->file('image');
// Validate the uploaded image
$validatedData = $request->validate([
'image' => 'required|image|max:2048', // Max size in KB (2MB)
]);
try {
// Store the original image in the public directory
$imageName = $image->hashName();
$imagePath = $image->move(public_path('admin' . $ds . 'uploads' . $ds . 'category_images'), $imageName);
// Create a resized thumbnail (100x100 pixels)
$thumbnailName = $imageName;
$thumbnailPath = public_path("admin" . $ds . "uploads" . $ds . "category_thumbnails");
if (!file_exists($thumbnailPath)) {
mkdir($thumbnailPath, 0777, true);
}
$thumbnail = Image::make($imagePath)
->fit(100, 100);
$thumbnail->save($thumbnailPath . "" . $ds . "" . $thumbnailName);
// Destroy the Intervention Image instance to free up memory
$thumbnail->destroy();
$thumbnailPath = $thumbnailPath . "" . $ds . "" . $thumbnailName;
$validatedData['image'] = $imagePath;
$validatedData['thumbnail'] = $thumbnailPath;
return redirect()->back()->with('status', 'Created successfully');
} catch (\Exception $e) {
return redirect()->back()->with('status', 'An error occurred while processing the image.');
}
}
Upvotes: 1
Reputation: 1
The problem is with the permission, just go to your directory and give it the read and write permission, your problem will be solved
Upvotes: 0
Reputation: 618
If you haven't created your path's folder, you will get that error. You need to create first it.
Upvotes: 5
Reputation: 104
In my case, it was not having permission on the folder, chmod with 755 did the job.
chmod -R 755
Upvotes: 1
Reputation: 131
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
Upvotes: 0
Reputation: 31
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").
Upvotes: 3
Reputation: 2126
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);
}
Upvotes: 37
Reputation: 8413
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.
Upvotes: 3
Reputation: 146191
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.
Upvotes: 18