Upload Images to Laravel-Lumen API - php

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

Related

laravel upload image in directory with custom permission

I am working on a website that uploads images in sub dir, for example, every time the user uploads an image gets stored in storage/dropzone/upload/timestamp_date/image_name.jpg the problem I faced is the image is not showing up on the client side when I looked on my shared hosting I found that stores function to create a directory with no executable to world permission 700 and I wanted to be 755 or 777.
I tried to change the permissions of my shared hosting files but it works only once cuz when a new directory is created will create it with 700 permission
You can create a helper function for common use inside whole application.
use Illuminate\Support\Facades\File;
function checkFolderExists($folder, $permission)
{
if (!File::isDirectory(base_path('storage/' . $folder))) {
File::makeDirectory(base_path('storage/' . $folder), $permission , true, true);
}
return 'public/' . $folder;
}
where
$folder = 'dropzone/upload/timestamp_date'
$permission = 0777 or 0775 or your_choice
And after check the store folder and permission your file store path with store image in that specific folder :
$filePath = checkFolderExists('dropzone/upload/timestamp_date', 0775 or 0777 or your_choice);
$fileStoredPath = $request->file('form_name_field')->store($filePath);

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

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.

Unable to upload image in Laravel

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.

Symfony2: Unable to access the web directory from the entity class

I am trying to use the cookbook example on how to upload fiels with symfony2 :
http://symfony.com/doc/2.0/cookbook/doctrine/file_uploads.html
The problem is that I am not able to get the path of the web folder.
Here are some details: In my web folder, I have a structure like that:
/web
/bundles
/sciforumversion2
/images
And from the entity, I I trying to get the folder path and save my image in the /images folder
For that, I am using:
protected function getUploadRootDir()
{
// the absolute directory path where uploaded
// documents should be saved
return __DIR__ . '/../../../../web/'.$this->getUploadDir();
}
protected function getUploadDir()
{
// get rid of the __DIR__ so it doesn't screw up
// when displaying uploaded doc/image in the view.
return 'sciforumversion2/bundles/images';
}
But I am getting then the exception that the directory can't be created:
Unable to create the "/home/milos/workspace/conference2.0/src/SciForum/Version2Bundle/Entity/../../../../web/sciforumversion2/bundles/images/conference" directory
Any idea on how could I get the web folder url and why the solution proposed by the symfony cookbook is not working is mostly welcome.
Thank you very much.
Thank you.
The path seems to be correct.
Probably you should give it the correct permissions.
Try this first:
chmod 777 /home/milos/workspace/conference2.0/src/SciForum/Version2Bundle/Entity/../../../../web/sciforumversion2/bundles/images/
If that works, it's a permission issue.
Set the owner and group to www-data using:
chown www-data:www-data /home/milos/workspace/conference2.0/src/SciForum/Version2Bundle/Entity/../../../../web/sciforumversion2/bundles/images/
chmod 755 /home/milos/workspace/conference2.0/src/SciForum/Version2Bundle/Entity/../../../../web/sciforumversion2/bundles/images/

Categories