GemmaM
GemmaM

Reputation: 21

Can't write image data to path laravel live project

On my project the user is able to upload a profile picture, and if they don't there is a default picture. I am using this on my Laravel project, and works 100%. Now I have hosted my project online it wont allow me to upload a profile picture but picks up the default image. I'm not sure why. The error message i'm getting is:

Intervention\Image\Exception\NotWritableException Can't write image data to path (/home/n1huer/laravel/public/uploads/avatars/1525256072.png)

This is my file structure within laravel

My controller for this is

public function update_avatar(Request $request)
{
    if($request->hasFile('avatar')){
        $avatar = $request->file('avatar');
        $filename = time() . '.' . $avatar->getClientOriginalExtension();
        Image::make($avatar)->resize(300,300)->save( public_path('/uploads/avatars/' . $filename) );

        $user = Auth::user();
        $user->avatar = $filename;
        $user->save();
        
    }
    return view('myaccount', array('user' => Auth::user()) );
}

The file path must be right if it is picking up the default?

Any help would be greatly appreciated, thanks

Upvotes: 1

Views: 7151

Answers (4)

Saddan
Saddan

Reputation: 1655

I have the same problem while using Image intervention library. It was working well in my local machine but in aws ubunto machine its giving this error. I have tried in many ways but finally I determined this all about permission issue. I'm using below codes to save an image from $request then resize it and then save it in profile folder under public directory.

$img=Image::make($avatar)->resize(300,300);
$img->save(public_path('profile/' .$file_storage_name));

what I see in my aws my profile folder is owned and grouped by root user. So I changed the ownership using sudo chown -R 1000:1000 profile and then changed the group using sudo chgrp www-data profile then give the folder read, write and execute permission using chmod 775 profile .Then magic ,it work .It look like below image enter image description here

Upvotes: 1

Hesham Mohamed
Hesham Mohamed

Reputation: 11

For whom this problem occurs:

First you need to know why this problem occurs

- See this path

path (/home/n1huer/laravel/public/uploads/avatars/1525256072.png)

This path is considered this way

http://myname.com/home/n1huer/laravel/public/uploads/avatars/1525256072.png

But it is intended here to be the actual in this

http://myname.com/uploads/avatars/1525256072.png

That is why the problem with the mentioned message occurs

to solve this problem, all you have to do is complete the following

come on this part

->save( public_path('/uploads/avatars/' . $filename) )

and replace it with this code to become like this

->save('./uploads/avatars/' . $filename)

and thus the problem is solved without the need to do anything else that may leave another problem leaving it

Upvotes: 1

The Billionaire Guy
The Billionaire Guy

Reputation: 3552

You can also use the command

sudo chmod -R a+rwx /path/to/folder

to give permissions to a folder and every file and folder inside it. Omit the x from either command if you don't want the files to be executable.

feast your soul here

Happy coding...

Upvotes: 1

Giovanni S
Giovanni S

Reputation: 2110

You need to make '/uploads/avatars/' writable as the error is saying that it isn't writable.

Log into the remote server (or use a control panel) and change the permissions to the folder you're trying to write to:

chmod 755 /home/n1huer/laravel/public/uploads/avatars

On a side note, I would look further into public folders and permissions.:

Here is some good advice:

https://stackoverflow.com/a/37266353/650241

Also, check out the official docs:

https://laravel.com/docs/5.6/filesystem

Upvotes: 1

Related Questions