Laravel 8 - Get image url from images saved in storage folder - laravel

I am using Laravel Framework 8.62.0 and save uploaded images like the following:
$contents = file_get_contents($imgUrl);
// $file_name = basename($url_upper);
$file_name = strtolower(substr($imgUrl, strrpos($imgUrl, '/') + 1));
$strg = Storage::disk('public_collectible_img')->put($file_name, $contents);
My public_collectible_img-driver looks like the following:
'public_collectible_img' => [
'driver' => 'local',
'root' => storage_path('app/public/collectibles_img'),
'url' => env('APP_URL').'/collectible/image',
'visibility' => 'public',
],
When I try to open the following url localy I get a 404-error.
http://localhost/my_project/public/collectible/image/1.jpg
The image exists in the folder:
How to access the image and what is the correct url?
I appreciate your replies!

You have to create the a symbolic link between "storage" and "public" folder
To make these files accessible from the web, you should create a
symbolic link from public/storage to storage/app/public.
Run the following artisan code:
php artisan storage:link
Reference: https://laravel.com/docs/8.x/filesystem#the-public-disk

Related

Laravel file from storage not found

Filesystem settings
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
Upload file
$path = $request->file('file-upload-photo-profile')->store('public/profile');
$user->photo_profile_url = Storage::url($path);
Patch to folder with file:
storage/app/public/profile
Example url:
Try get image:
<img src="{{asset( $user->photo_profile_url)}}" >
Generated url:
http://sakura/storage/profile/aDtjR6z7Ih4fkmo7bdAqqyDXmhu2qE4pg3s5BBla.jpg
But I got 404 for this image.
In server settings root dirrectory
public
How do I get a link to an image?
Do you have the symlink to the storage folder in your app/public directory?
Like so: php artisan storage:link

how to upload images in public_html not in project file in laravel?

I upload my Laravel app to C-panel, my public contents are in public_html and I change index.php on public to this :
require __DIR__.'/../thermotajhiz/vendor/autoload.php';
$app = require_once __DIR__.'/../thermotajhiz/bootstrap/app.php';
And other files in main root named thermotajhiz
Everything is ok but when I want to upload an image, instead of upload to public_html, a file with public name will create in thermotajhiz file outside the public_html
how can I fix this ?
I'm using this plugin for upload files :
alexusmai/laravel-file-manager
in config/filesystems.php set like this
// Filesystem Disks
'disks' => [
'public' => [
'driver' => 'local',
'root' => '../public_html',
'url' => env('APP_URL'),
'visibility' => 'public',
],
],
if doesnt work try to change ../public_html to ../../public_html or /home/yourcpanelusername/public_html
and also make sure in your config/file-manager.php you set:
'leftDisk' => 'public',
I put this code to index.php instead of register method in AppServiceProvider and it's work :
$app->bind('path.public', function() {
return base_path('../public_html');
});
After this code :
$app = require_once __DIR__.'/../thermotajhiz/bootstrap/app.php';

LARAVEL 7 - Image not showing after config:clear

I'm working on Laravel 7 (using Laragon 4.0 on Windows 10), I have some file download and save from remote urls. Everything was working fine but I had to perform a "php artisan config:clear" to update mysql configuration and after that the images saved in storage/public folder are not served anymore.
The files were there but just not served, then I deleted everything inside /storage/public folder, deleted the symlink inside /public folder to /storage (naturally performing "php artisan storage:link") and populate again /storage/public with folders and file like I did previously. The folder structure is there, the files also but when I try to access them I get 404 error.
I tried "php artisan optimize:clear" and every suggestion I found on stackoverflow but nothing seems to help me, I don't know where to look, please help me.
Here is the code I use to download/save images:
$url = $team["logo"];
$contents = file_get_contents($url);
$name = substr($url, strrpos($url, '/') + 1);
$file = 'public/team/logo/'. $name;
Storage::put('team/logo/'. $name, $contents);
This is how I display images on the view:
<img src="{{Storage::url($team->logo)}}" style="max-width:50px">
This is my public disk configuration on filesystems.php
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
As your driver is set to
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
Your Storage::disk('local') is, in absolute terms, set to /storage/app/public. Hence $file = 'public/team/logo/'. $name; becomes $file = 'team/logo/'. $name;
In case you are using the Intervention/Image library, you may consider to change
$contents = file_get_contents($url);
to
$contents = File::make($url);
see http://image.intervention.io/api/make

How i can upload image to public folder in shared hosting laravel 5

I moved my website from localhost to shared hosting all it's good, but i need when i upload file stored directly to public_html: something like this
public_html/storage/
I tried use somenthing like :
symlink('/home/abdoweb/bmcelarave/storage/app/public', '/bmce/public_html/storage')
the problem still exists.
my Controller :
public function addcategory(Request $request){
$title = $request->input('category_name');
$image = $request->file('category-image');
$name = $image->getClientOriginalName();
$image->move(storage_path().'/app/public/category',$name);
$data[] = $name;
$query= DB::table('category')->insert(
[
'title' => $title,
'image' => $name,
"created_at" => Carbon::now()
]);
if($query){
return redirect('categories');
}
}
My folder :
home/abdoweb/{bmcelaravel} <= my public folder
Core laravel :
home/{bmce} <= core laravel
Thank you.
You can a use storage driver :
Inside config/filesystems.php :
'disks' => [
'public' => [
'driver' => 'local',
'root' => public_path() . '/uploads',
'url' => env('APP_URL').'/public',
'visibility' => 'public',
]
]
//Now you can move the file to storage like :
Storage::disk('public')->putFile('category', $request->file('category-image'));
First of all the recommended location for that stuff is to stay on the public path not creating a new one, unless there is an actual reason for that. Did you actually check that the symlink was created?
Laravel has an own command the create a symlink from storage/app/public to public/storage (the storage folder will be generated afterwards):
php artisan storage:link
But if you want to create defaults symlinks you should create one for yourself, like you already did.
This is the symlink pattern:
ln -s target source (fill in your target and source path)
So if you actually get the correct file from your request, this code should work:
Storage::putFile($name, $request->file('category-image'));
For more and detailed infos look into the filesystem documentation

404 Page Not Found When Viewing Images Laravel

I have an image stored in the database as conventions/convention_title.jpg
The file is located in "storage/app/conventions/convention_title.jpg"
When i do something like this flyer }}">, it doesn't display...
OR
flyer }}"> the image still wont display...
And even worse still, if i try to access the image directly via link like this...
http://localhost:8000/storage/app/convention/convention_title.jpg i get a view not found error -> 404 page not found..
What could be the cause of this?
php artisan storage:link
problem solved while writing
src=" {{ asset('/storage/'.$post->image) }}"
Web content must be stored on the laravel root /public folder and you can easily access to it via asset helper.
For example:
/public/img/conventions/convention_title.jpg
Then you can access on the views to it like:
{{ asset("/img/conventions/convention_title.jpg")}}
In case you want to test if you can access to the image you can try:
http://localhost:8000/convention/convention_title.jpg
You can create a new disk in the config/filesystems.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path(),
],
'uploads' => [
'driver' => 'local',
'root' => public_path() . '/uploads',
],
]
Then use it :
Storage::disk('uploads')->put('filename', $file_content);
Or you can do this simple method
$request->file('photo')->move(public_path("/uploads"), $newfilename);
Then, using any of the above methods, you can still access your images using the asset() helper function...

Resources