Image retrieving failed with laravel 5.4 - php

I'm trying to store and show image for my app. Image storing properly but failed to show.
First I created symbolic link using command: php artisan storage:link
config/filesystem.php look like:
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
to store image:
if($request->hasFile('avatar'))
{
$path = $request->file('avatar')->store('avatars');
//$path=avatars/generated_image_name.extension
$user->avatar=$path;
$user->save();
}
//image storing under: (../storage/app/avatars)
to display image to view:
<img src="storage/{{$user->avatar}}"
alt="no image!!"
class="img-circle"
style="width:150px;height:150px;">
tried src="storage/app/{{$user->avatar}}" & src="{{$user->avatar}}"
but can't fetch image. Don't understand what I'm doing wrong.

Hate to be that guy, but have you read the docs. You need to create a symbolic link from public/storage to storage/app/public.
The Public Disk
The public disk is intended for files that are going to be publicly
accessible. By default, the public disk uses the local driver and
stores these files in storage/app/public. To make them accessible
from the web, you should create a symbolic link from public/storage
to storage/app/public. This convention will keep your publicly
accessible files in one directory that can be easily shared across
deployments when using zero down-time deployment systems like
Envoyer.
To create the symbolic link, you may use the storage:link Artisan
command:
php artisan storage:link
Edit
Again I would check out the docs or maybe read them :).
I think your default storage is local and not public. In config/filesystems.php look at the default driver.
Storing Files > File Uploads > Specifying A Disk
or try something like this.
if($request->hasFile('avatar'))
{
$path = $request->file('avatar')->store(
'avatars/'.$request->user()->id, 'public'
);
//$path=avatars/generated_image_name.extension
$user->avatar=$path;
$user->save();
}

Related

disk [users] not configured, please add a disk config in `config/filesystems.php`

PHP version PHP/8.1.10
Laravel version 9.28.0
While working on a Laravel-admin backend, I tried creating a new user (basically inserting data into admin_user table in the database)
working on my windows computer, I encountered the error:
disk [users] not configured, please add a disk config in config/filesystems.php.
I revisited the Laravel docs and these are the things I did to solve the problem:
Opened the config/admin.php and added:
'disk' => 'users',
// Image and file upload path under the disk above.
'directory' => [
'image' => 'images',
'file' => 'files',
],
Then opened the config/filesystems.php and added:
'users' => [
'driver' => 'local',
'root' => storage_path('app'),
]
Closed my git bash and reopened it, ran:
php artisan serve
Accessed the page for creating a user and the error was solved..
Do you know any other way this could be solved?
To use a custom disk you must configure it first. To configure a disk, follow the following steps:
Open filesystems.php file, under config directory, in your preferred text editor or IDE.
Look for the key named disks. The array under disks is where you configure your disks. Laravel already provides some disks out of the box (I recommend not messing with them unless you know what you are doing).
Add your new disk configuration, something like:
'users' => [
'driver' => 'local',
'root' => storage_path('app/public/users'),
'url' => env('APP_URL').'/storage/users',
'visibility' => 'public',
]
Note: The above example assumes you have already executed php artisan storage:link command in order to create a symbolic link that allows the files in storage/app/public to be accessible from outside.
Save the file and now you can use that new disk like below:
use Illuminate\Support\Facades\Storage;
// initialize a new disk using the `users` disk config
$disk = Storage::disk('users');
// perform actions under that disk
$disk->delete('sub/folder/file.ext');
Also, it is important to note that you are not limited to have disks under the storage folder, you can actually have disks under the public folder:
'users' => [
'driver' => 'local',
'root' => public_path('/users'),
'url' => env('APP_URL').'/users',
'visibility' => 'public',
]
Note that the function public_path is used instead of storage_path. Also the command php artisan storage:link is not needed when your disks are under the public folder.
Learn more about File Storage in Laravel.

Laravel Nova uploaded image file not showing on staging environment

I have deployed a laravel application which is using the Nova admin for backend management on Bluehost VPS account. I have creates a symlink between storage and public as recommend in the laravel documentation.
I have set up a resource for uploading images like so
Avatar::make('Image')->onlyOnIndex(),
Image::make('Image')->disk('public')
->path('item-category')->required()->hideFromIndex()
I am using the public disk for storage with the filesystem disk configuration like so
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
On my local system, I am able to see the actual image displayed but on the staging the image is uploaded successfully but does not display because the image is not found
The record is stored in the db like so
Why does this same configuration work on my local dev system but not on the staging server? Spent the best part of my morning trying to figure this out.
Try running the artisan command php artisan storage:link, that will create a symlink from your public folder to the storage folder.

Using storage_path from another Laravel app

I have an application that uses two different Laravel apps talking to the same database. App 1 is called BUILDER and App 2 is called VIEWER. In production I use S3 for storing files submitted within the application. For local development I use the storage/app/public folder in BUILDER.
The local dev setup is that BUILDER runs on localhost:8000 and VIEWER on locahost:8001
Now here comes my problem. In production both apps use the same S3 bucket for storage. So somehow I need to set this up similarly for local development.
The BUILDER is working fine, uploading and reading its files from the storage/app/public folder with FILESYSTEM_DRIVER=public in .env
The VIEWER is also reading these files fine, creating correct URL's after I added a new disk in the config (BUILDER_URL is set in .env to localhost:8000 which is the URL for the BUILDER)
'builder_public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('BUILDER_URL') . '/storage',
'visibility' => 'public',
],
BUT... I need to somehow be able to also upload files from the VIEWER app that should end up in the same storage folder as the BUILDER.
So in my VIEWER app I would like my builder_public disk to be something like this:
'builder_public' => [
'driver' => 'local',
'root' => builder_storage_path('app/public'), // here
'url' => env('BUILDER_URL') . '/storage',
'visibility' => 'public',
],
Is there some way I can share the storage/app folder between two separate Laravel apps?
Yes, you can. if you are using Linux server, try below command
ln -s SOURCE_FOLDER DESTINATION_FOLDER
it will create folder short cut but still, your both application will use the same location(DESTINATION_PATH)
If you are using different servers, try mount command.

Change default "symbolic link" folder in laravel 5.4 (storage folder)

I'm making a new version of my site with laravel 5.4 and want to keep the same links than my old site has.
My old site has links images like this:
domain.com/uploaded/cat.jpg
In laravel, its necesary to create a symbolic link to show the stored images but it saved them with a default folder named storage", like this:
domain.com/storage/cat.jpg
How can i change the default name to "uploaded" or whatever?
I tried to change url inside the file \config\filesystems.php with this:
Before
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
After
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/uploaded',
'visibility' => 'public',
],
and my controller
$request->file('image')->storeAs('public', 'example.jpg');
Make sure you've created a symlink.
According to your configuration, you can run this command :
ln -s storage/app/public public/uploaded
Then, you can access your files :
Storage::disk('public')->url('example.jpg')
Or simply using asset() helper :
asset('uploaded/example.jpg');
Execute this code in your server using any php file to create the symbolic link. Once the link has been created, the files will become publicly accessible.
target = '/home/cfrcoclm/public_html/agencies/storage/app/public'
shortcut = '/home/cfrcoclm/public_html/agencies/uploaded'
symlink(target, shortcut)
ec ho("Link Created!")
`
In case you do not have SSH access, you can as well write this one-line script below and run it on your server - taking into consideration the right directory path
symlink('/home/domainname/laravelprojectname/storage/app/public','/home/domainname/public_html/storage');

Download file from a link in laravel 5.3

I want to create a hyperlink to download a file in my storage/app/public folder. I am using this in the blade
Read Paper
But this show me the error file not found.What should i do
do
this is my filesystem.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'visibility' => 'public',
],
An easy way that allow you to store your files in the storage folder and allow to download without system changes: (in laravel 5.3)
You can make a normal link to the file:
Read Paper
Then you need to configure your routes/web.php
Route::get('/download/{file}', function ($file='') {
return response()->download(storage_path('app/public/'.$file));
});
This should open the download dialog for all the kinds of files, also for img's and pdf's. If you want to show the files online and not to download you should use the file method:
return response()->file(storage_path('app/public/'.$file));
Asset paths are relative to the /public directory. So you would just want asset('1489082996.docx') assuming you've properly sym linked your storage directory with php artisan storage:link.

Categories