Patrick Obafemi
Patrick Obafemi

Reputation: 1066

NotWritableException Can't write image data to path on heroku

I am trying to save my image data to a folder. It works fine on my localhost but when i moved it to heroku it gives me this error

NotWritableException
Can't write image data to path (https://arcane-peak- 
34431.herokuapp.com/public/images/categories/1527155055.jpg)

Here is my code.

$image = $request->file('image');
    $imagename = $image->getClientOriginalExtension();
    $imagename = time().'.'.$imagename;

    $destinationPath = URL::to('public/images/restaurants');
    $img = Image::make($image->getRealPath());

    $img->resize(100, 100, function ($constraint) {
        $constraint->aspectRatio();
    })->save($destinationPath.'/'.$imagename);

    $destinationPath = URL::to('public/images/restaurants');

    $image->move($destinationPath, $imagename);

What am i doing wrong? And also on my local machine it is the uploaded image that is getting saved but i want my resized image to be saved.Any help would be a life saver

Upvotes: 0

Views: 435

Answers (3)

Good Muyis
Good Muyis

Reputation: 147

In my case, the git push heroku main command excludes the image destination from being uploaded because it was empty. So in this case public/images/categories will not exist in the heroku app server.

You can run heroku run bash -a <APP_NAME> to check the app folder if the public/images/categories does exist online or not.

To prevent the error from future occurrence, you can add an empty index.php file in the folder before you run git push, this will make sure the folder gets uploaded.

Upvotes: 1

AbdulRahman Atef
AbdulRahman Atef

Reputation: 43

You can try

Image::make($image)->save('images/restaurants/' . $imagename);

This works for me

Upvotes: 0

Chris
Chris

Reputation: 136977

The bigger issue is that even if you get your image to save it will disappear. Heroku uses an ephemeral filesystem (bold added):

Each dyno gets its own ephemeral filesystem, with a fresh copy of the most recently deployed code. During the dyno’s lifetime its running processes can use the filesystem as a temporary scratchpad, but no files that are written are visible to processes in any other dyno and any files written will be discarded the moment the dyno is stopped or restarted. For example, this occurs any time a dyno is replaced due to application deployment and approximately once a day as part of normal dyno management.

Heroku recommends storing user uploads using a third-party service like Amazon S3.

Upvotes: 1

Related Questions