Unable to upload image in Laravel - php

I am working on laravel 5.3.30 and using image intervention helper for uploading image. I am getting error "NotWritableException in Image.php line 143: Can't write image data to path (/home/test/Desktop/Laravel/blog/public/images/)" while uploading image. I have set appropriate permission to the directory but still am not able to upload image.
Code to upload image file:
if($request->hasFile('featured_image')){
$image = $request->file('featured_image');
$filename = time().'.'.$image->getClientOriginalExtension();
$location = public_path('images/', $filename);
Image::make($image)->resize(800,400)->save($location);
$post->image = $filename;
}
I have checked the solutions online but none it worked. Please correct me if I am doing something wrong. Thanks in advance.

Here is alternative approach. Consider storing in storage directory, not in public. This is Laravel recommended way.
It may be storing inside storage/app/public/images directory. And then create symlink with php artisan storage:link command.
This way when storing use this path:
$location = storage_path('app/images/', $filename);
After symlink is created, you may access image url like
http://your-site.app/storage/images/qwerqrewqerwq.jpg

Run this command to fix permissions:
chmod -R 755 /home/Desktop/Laravel/blog/public
If it'll not work and if it's a local machine, try 777.

Related

Move images into a folder in the public folder in Laravel but fails

I have three folders (userImages, productImages and clientImages) inside the images folder with the public folder in a Laravel(9.5.1) project. I am trying to upload images and move same into the productImages folder but it is not working. However, if I try with the userImages folder, it works and works only with that folder. I would be glad for any assistance from you. Thank you for your assistance in advance.
This is code:
if($request->hasFile('product_image')){
$image = $request->file('product_image');
$newImageName = uniqid().'-'.Str::slug($request->product).'.'.$image->getClientOriginalExtension();
$location = public_path('/images/productImages');
$image->move($location, $newImageName);
}else {
$newImageName = 'default_image.png';
}
The error message I get from the above line of code is:
The stream or file "/Applications/XAMPP/xamppfiles/htdocs/pos/storage/logs/laravel.log" could not be opened in append mode: Failed to open stream: Permission denied The exception occurred while attempting to log: The stream or file
You need to be specific with the directory you want to allow the permission. Like below for your case.
sudo chmod -R 777 /Applications/XAMPP/xamppfiles/htdocs/pos/public/images/productImages
Please make 777 for storage folder permissions. Laravel tries to log the error to the log file. But storage folder permissions don't let it. Then you will see what is wrong with your code in the log file.
sudo chmod -R 777 storage

Laravel 8 Storage move file not found

I am trying to move a file from a temporary path to the correct path. However no matter what I tried it always sends back this file not found error:
League\Flysystem\FileNotFoundException
File not found at path: var/www/html/storage/app/public/covers/tmp/608c07a082451-1619789728/81-536x354.jpeg
I am using sail thus there are 'var/www/html/' in the path, even though there is no problem with this path in the container since it is possible to reach this path from the frontend fetch API and that is how the temp file was saved.
Here is the code in the controller:
//...
$temp_file = TempFile::where('folder', $cover_folder)->first();
if ($temp_file) {
// retrieve file path and target path
$filepath = storage_path("app/public/covers/tmp/{$temp_file->folder}/{$temp_file->filename}");
$target_path = storage_path("app/public/covers/{$course->id}/");
Storage::move($filepath, $target_path);
$course->cover_img = "{$target_path}/{$temp_file->filename}";
$course->save();
// remove temp directory and temp file record
rmdir(storage_path("app/public/covers/tmp/{$temp_file->folder}/"));
$temp_file->delete();
}
The error traceback highlighted on this line:
Storage::move($filepath, $target_path);
Also tried to use Storage::disk('public')->move(), or public_storage, or Storage::path(), or only pure string, all did not work.
Symlink is set from public/storage to storage by running php artisan storage:link.
I have searched as much as I could find on laracast, stack overflow, or github, but could not reach a solution. Does anyone have any idea on this issue?
Really really appreciate it.
dont use storage_path function because method Storage::move( have prefix path [app_path]/storage/app/
Storage::move("public/covers/tmp/{$temp_file->folder}/{$temp_file->filename}", "public/covers/{$course->id}/");

How to undo php artisan storage:link command that deleting all storage folder content

I uploaded an image file using this code.
// Get filename with the extension
$filenameWithExt = $request->file('cover_image')->getClientOriginalName();
// Get just filename
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
// Get just ext
$extension = $request->file('cover_image')->getClientOriginalExtension();
// Filename to store
$fileNameToStore= $filename.'_'.time().'.'.$extension;
// Upload Image
$path = $request->file('cover_image')->storeAs('public/cover_images', $fileNameToStore);
Then tried to make a symlink to the storage folder so it can be accessible from the public folder. I typed this command php artisan storage:link in the cmd, But then it destroyed all the contents in the storage folder and deleted it then displayed this error.
The system cannot find the path specified.
The [public/storage] directory has been linked.
Now when I run the server PHP artisan serve I get this error.
I am new to laravel and now am stuck, how can I solve this? The storage folder was recreated with nothing but log files in it.
I was actually able to solve this error. The good thing is that I had a copy of the same project in another directory, so I delete the current storage folder showing log files only and the copy and pasted the storage folder from the other project I had backedup and re-uploaded the image. After that, I used the same command php artisan storage:link but this time I had the public folder path selected
C:\xampp\htdocs\mwalimu_shaaban\public
C:\xampp\htdocs\mwalimu_shaaban>php artisan storage:link
The [public/storage] directory has been linked.
I Think the problem arouse since I had not selected the symbolic link destination path in the beginning.

Laravel images not Writeable created with Intervention/image

I am working With Intervention/Image, It create some images. when i try this from saved path,
Image::make(storage_path('app/'.$photo->path));
gives,
Image source not readable.
first check to see if you have permission problems then check to see if you have done php artisan storage:link
Thank you brother! I just got it,
it is permission's problem, where Intervention/Image object was creating folder while storing file to related path.
$DesPath = "uploads/photos/u{$user->id}/{$dateFolder}/p{$ran}/";
The solution is to create those directories in the path before save file in it.
so i did this before $img->save() and it worked!.
Storage::disk('local')->makeDirectory(storage_path('app/'.$DesPath));
before
$img->save(storage_path('app/'.$DesPath).$filename);

Upload Images to Laravel-Lumen API

With my API i need to upload images from mobile app to my server and save the image path to database so i have the following issues :
Where to save the images? I tried to save them in the storage/app
under images folder (which is work fine)
public function fileUpload(Request $request) {
if ($request->hasFile('image')) {
$image = $request->file('image');
$name = time().'.'.$image->getClientOriginalExtension();
$destinationPath = storage_path('/app/images');
$image->move($destinationPath, $name);
return response()->json(['data'=>"image is uploaded"]);
}
}
and make a symbolic link to this folder in the public folder but its
not working due to access permission error which will lead to the
other issue.
What permission should i gave to the storage folder to make the whole
operation works save the images and the saved link is readable (even 777 didn't work) Access forbidden!
sudo chmod -R 777 storage
Any ideas will be much appreciated
Storage directory is not open to user's requests, public directory is, you need to create a symbolic link from storage directory to public directory:
php artisan storage:link
This command will create a symbolic link from public/storage to storage/app/public, in this case you can store your file under storage/app/public and make it accessible from the web too:
$image = $request->file('image');
$image->storeAs('public', $name); // => storage/app/public/file.img
URL::asset('storage/'.$name); // => http://example.com/stoage/file.img
I would advice you try to create a folder in the public folder and store your files there. You can use base_path()."/public/uploads"
The best place to store the images would inside the storage folder and create a symbolic link in the public folder.
sudo chmod -R 777 storage should work. Make sure you are accessing the right url.
To access the image you should use something like the below urls. My folder structure looks like /storage/app/public/images/user/avatar/
Local: http://localhost/project-name/public/storage/images/user/avatar/HdXzVnKxzUSdAssvXaoM2n0ixzEEG7jlK8acLiHV.png
Production: http://example.com/storage/images/user/avatar/3133L07guCv8shINNZM30c2Ux1HdfpFt04YdLRpf.png
You can check this gist to figure out if you are missing something: https://gist.github.com/danish-shaikh/9c328288a817309f93238c6b5d48ed7e

Categories