27

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

1
  • 1
    Nice code you've written there. This Error seems to be an issue with a directory. The code is unable to find that directory. You can solve that by using a relative path to specifically point to that directory for example in my case I had used the path public/baz.jpeg and got same error as you'rs. I changed the path to ../public/baz.jpeg and now Image is stored there as expected. Commented Sep 20, 2021 at 6:26

10 Answers 10

37

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);
        }
Sign up to request clarification or add additional context in comments.

3 Comments

I couldn't understand the problem at first but it solved my image upload issue to public image path. Thanks!
shouldn't the permission be 775 instead of 666?
thought so, it was supposed to be 775
18

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.

5 Comments

how do I make sure the path/location is writable and has write permission ?
What OS your project is running on ?
windows 8 I am running
to make sure that the folder is writable, use the following command: sudo chmod 775 your_directory for linux
5

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

1 Comment

solved my error, i had a wrong file name
3

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.

Comments

3

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 Comment

Welcome to stack overflow, this site accepts answers in English only.
1

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

Comments

1

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

chmod -R 755

1 Comment

please elaborate a little bit the answer showing how to achieve this. Possibly show how to change the code of the OP to implement the change you suggest.
1
   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.');
        }
    }

Comments

0

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

Comments

0

The problem is with the permission, just go to your directory and give it the read and write permission, your problem will be solved

1 Comment

This does not improve on any of the answers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.