Can't write image data to path laravel live project - image

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

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

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...

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

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

Related

Issue getting a file with Laravel

Hi I do not know what is happening but I want to get a file from the storage folder and it says that it does not existe but it exists I mean I go to the folder and it is there..
I retrieve the file like this:
$path = storage_path('app/public/' . $settlement->support);
$settlement_file = Storage::disk('local')->get($path);
And I get this error:
message: "File not found at path:
home/jysparki/newjis.jisparking.com/storage/app/public/06152617_8-2025.pdf"
and If I go to the folder it exists so I wonder what am I doing wrong? Thanks.
If you look at config/filesystems.php file and find disks array, you may notice that root under the local disk key is set to storage_path('app'). Well it's up to what the value may be in your case you're free to change it, but what does this mean is whenever you call Storage::disk('local') method it will try to find files relative to storage_path('app'), so there is no need to specify full path. Try this instead
$settlement_file = Storage::disk('local')->get('/public/' . $settlement->support);
or
$settlement_file = Storage::disk('public')->get($settlement->support);
as Laravel by default has public disk which pointed into /app/public storage directory

Laravel File not found at path (again)

So, I've already dug through the questions here about this, but still can't seem to figure it out.
Do you know what I am doing wrong here?
$imageName = $thishire->color_image;
$path = public_path('/img/colorImages/' . $imageName);
$file = Storage::get(public_path('/img/colorImages/'. $imageName));
$file->move($path, public_path('/img/uploads'));
The 3rd line is throwing an error:
"File not found at path: Applications/MAMP/htdocs/GT Laravel/public/img/colorImages/l18Ow1uVBqhReGqW7lWjanp2vT5bOAKt8pylnmmb.jpg"
But that is the correct path, so I'm super confused.
You shouldn't do Storage::get(public_path('/img/colorImages/'. $imageName)).
Storage::get() is relative to the storage disk root.
If you use the default storage configuration, then your storage root path is storage_path('app'), said otherwise /path/to/your/project/storage/app.
This is what's happening:
Storage::get(public_path('/img/colorImages/'. $imageName));
// looking into /path/to/your/project/storage/app/path/to/your/project/public/img/colorImages
// which is obviously wrong
What you should do instead:
Store your public files in the storage/app/public directory
Use the php artisan storage:link command to create a symlink between public and storage/app/public. If you are not familiar with this concept, it'll create a small file into your public directory which is a shortcut to the storage/app/public directory. More about this here: https://laravel.com/docs/8.x/filesystem#the-public-disk
For instance, with a file named helloworld.jpg and located in storage/app/public/users/8/helloworld.jpg, you can get it by doing:
Storage::disk('public')->get('users/8/helloworld.jpg');
// or Storage::get('public/users/8/helloworld.jpg');
I know it can be confusing at first. This is a quick tip to "know where you are and what your are looking at". Use Storage::path():
dd(Storage::path(public_path('/img/colorImages/'. $imageName)));
// you'll see the problem
don't forget public meaning access storage but by created symlink
I think you should be store image in your path before getting it by using
Storage::put(public_path('/img/colorImages/'), $imageName);
and you can check documentation from here
I solved it.
$imageName = $thishire->color_image;
$old = public_path('/img/colorImages/'.$imageName);
$new = public_path('/img/uploads/'.$imageName);
rename($old, $new);

Laravel - File upload doesn't seem to allow all types of files for some reason

My application denies access to upload files with certain extensions for some reason.
It prints out the error:
fopen(C:\wamp64\www\storage\app\public/wrdjxF3EMbtGCUyXjSEWH5zeAu822ACeKikLPaCT.): failed to open stream: Permission denied
The file types that have given out this error so far are: .msg and .txt.
I have no clue what causes this problem, other file types seem to work fine.
The controller function that handles the file uploads:
if($request->hasFile('document'))
{
$files = $request->file('document');
foreach($files as $file){
if($file){
$name = $file->getClientOriginalName();
$size = $file->getClientSize();
$size = number_format($size / 1048576,2);
$path = $file->store('public');
Document::create([
'report_id' => $id,
'report_type' => "App\VisitingReport",
'file_path' => $path,
'file_name' => $name,
'file_size' => $size.' MB'
]);
}
}
}
Any help would be appreciated, thanks.
I'm running this on a Windows computer using WAMP.
I am convinced that it has something to do with the permissions indeed. I'm already a step further, the file gets uploaded but this time without the extension.
can you paste here what permissions you see for other file types and .txt after uploads. Run ls -la in the place where these files are stored and permission should be on far left. something like rwxr-wr-w. Paste here so that at least it can give a clue on what could be the issue.
Otherwise, once .txt file is uploaded you can run chmod 777 some.txt and see if you are still getting permission issue. If you aren't getting permission issue anymore than it's somehow creating .txt file with different permission. But it's not a good idea to do 777 for anything :D. But at least now you know what the problem is.
If permissions are the problem then here is a wonderful answer on this topic
How to set up file permissions for Laravel?
Good luck :)
I'm not an expert just trying to help
I have faced with this error, but I don't know it works for you or not.
put this in your .env file
FILESYSTEM_DRIVER = public
and link the public folder with storage folder with this command:
php artisan storage:link
I hope it works.

Laravel Permissions on Windows For Saving in Public Path

I am using intervention image to save images to the public path in a laravel project. My code is as follows
Image::make($image)->resize(300, 300)->save( public_path($path . $filename ) );
I have ensured that the directory exists yet still receive an error
Intervention \ Image \ Exception \ NotWritableException
Can't write image data to path
I have found various ways to fix this in a linux environment with chown however I see none for windows. How can I fix this?
put this before you save your image
if (!is_dir($path)) {
\File::makeDirectory($path, $mode = 0755, true, true);
}
but if you need best solution you need to use
\Storage::disk('public')->put(your_path,$image);
that error doesn't mean you need permission , especially in windows environment
that means you send invalid path parameter (directory not existed)
lets say $path like this
$path = 'assets/uploads';
and then $filename like this
$filename = 'foo.bar';
last part for saving the image
Image::make($image)->resize(300, 300)->save( public_path($path . '/' . $filename ) );

Edit file.php from controller using CodeIgniter

I need to add some content to a file from a controller in CodeIgniter, in my local enviroment I do this and works fine:
$handle = fopen(APPPATH."config/file.php", "a+");
fwrite($handle, $stuff);
fclose($handle);
However the file doesn't seem to get altered when it runs in the production environment server.
At first I though it could be permission issues but chmoding 777 the directory doesn't seem to work either
It seems to me APPPATH is pointing to a different path. Make sure APPPATH is correct.
Dumb of me, I chmoded the folder containing the file but not the file itself.

Resources