2

I am currently using the Intervention Image package within Laravel.

I am wanting a user to have the ability to upload a logo. So far, I have the following:

public function postUpdateLogo($id) {

    if(Input::file())
    {

        $image = Input::file('logo');
        $filename  = time() . '.' . $image->getClientOriginalExtension();

        \Image::make($image->getRealPath())
                  ->resize(300, 300)
                  ->save('user/'. $id . '/' . $filename);
        $user->image = $filename;
        $user->save();
    }
}

But the error I'm getting upon submission is:

NotWritableException in Image.php line 143: Can't write image data to path (user/1/1439491280.png)

Any help would be hugely appreciated.

  • It sounds like the directory you're trying to save to isn't writable (`777`). – Stuart Wagner Aug 13 '15 at 18:51
  • It also looks like you'll probably want to add some more info to the path, e.g. `'/path/to/images/user/'.$id. ...` – Joel Hinz Aug 13 '15 at 19:03
  • @JoelHinz, the `user/$id/randomString` would suffice I think? I don't think that's causing the problem though. –  Aug 13 '15 at 19:19
  • Well, yes, if you're trying to upload it to Laravel's root folder. Don't you want to put it in the public folder at least? – Joel Hinz Aug 13 '15 at 19:22
  • I thought that would default into the public directory? My intention was to put it in there though! –  Aug 13 '15 at 19:26
  • Based on the [Intervention docs](http://image.intervention.io/), it looks like you'll need to save to `public/user/...`. – Stuart Wagner Aug 13 '15 at 19:46
  • In that case, should be easy to fix the path: `->save(public_path('user/' . $id ...));` - of course, that's assuming the folder exists and is writable. :) – Joel Hinz Aug 13 '15 at 19:51
  • check those for inspiration http://stackoverflow.com/questions/30682421/how-to-protect-image-from-public-view-in-laravel-5/30682456#30682456 and http://stackoverflow.com/questions/31582829/laravel-doesnt-show-image/31584676#31584676 – Maytham Fahmi Aug 13 '15 at 20:35
  • Btw I do not suggest giving write permission to public folder, you can follow this link http://stackoverflow.com/questions/30682421/how-to-protect-image-from-public-view-in-laravel-5/30682456#30682456 to upload image, and if you want to make them public, then just remove 'middleware' => 'auth', – Maytham Fahmi Aug 13 '15 at 20:38

1 Answers1

1

I came across this problem too, as Stuard suggested you might want to write in the public folder.

$filename  = time() . '.' . $image->getClientOriginalExtension();
$path = public_path("user/".$id."/".$filename);

\Image::make($image->getRealPath())->resize(300, 300)->save($path);

Secondly I managed to fix my problem (ubuntu, apache, laravel 5 setup) by making apache the owner of that public folder e.g. :

sudo chown -R www-data:www-data /home/youruser/www/dev.site.com/public/user

add the right folder permission:

~$ sudo chmod 755 -R user

Might not be a perfect solution but will get you going.

Edit - a possible second option:

Checking the current group owner of your public folder and then adding apache user (www-data) to that group might be a second option (hope the gurus agree):

sudo adduser www-data theownergroup
stefan
  • 2,685
  • 2
  • 24
  • 31