In this article i will show you to access and setup uploaded storage files in laravel. This is strongly recommended that not to use direct URL from storage, laravel internally handle it by creating Symblink but some developers access the files of storage directly without knowing the consequences of website vulnerability. Sometimes we put public/index.php
in root folder which gives several access to hackers to hack easily to website so i recommend it to not to perform this activity rather than create virtual host or artisan serve command.
While using virtual host or artisan serve command we can’t access storage folder directly so to access the files we need to create Symblink first.
So let’s understand how to access and setup storage files after upload in laravel
This is an important step and i suggest you to not skip this step, if you are going to show images in your website because you must need to access the image to show it on website so open config\filesystems.php and add public_path('uploads') => storage_path('app/uploads')
to links block
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Filesystem Disk
|--------------------------------------------------------------------------
|
| Here you may specify the default filesystem disk that should be used
| by the framework. The "local" disk, as well as a variety of cloud
| based disks are available to your application. Just store away!
|
*/
'default' => env('FILESYSTEM_DRIVER', 'public'),
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
'endpoint' => env('AWS_ENDPOINT'),
'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
],
],
'links' => [
public_path('storage') => storage_path('app/public'),
public_path('uploads') => storage_path('app/uploads'),
],
];
Since we are using in our controller this code
$destinationPath= '/uploads/images';
$storageDestinationPath=storage_path('app'.$destinationPath);
so we created a Symblink as
public_path('uploads') => storage_path('app/uploads'),
Now run artisan command to run Symblink
php artisan storage:link
Or for shared hosting
Route::get('/artisan-link', function () {
Artisan::call('storage:link');
});
NOTE: If you are using php artisan serve
and virtual host
to serve your application then above method will work perfectly but in case you are putting your public/index.php in root folder then you need to add public in URL. Example
URL for php artisan serve
and virtual host
will be and this will work perfectly
http://localhost:8000/uploads/images/16525039733751202249.png
and you are putting your public/index.php in root folder then you need to add public in URL
http://localhost/projectname/public/uploads/images/16525039733751202249.png
You can check full example here :