Laravel Storage::disk()->url(); does not work properly - php

Hello i have a trouble in laravel.
i wanna create private storage for some stuff (xls,image,pdf, etc).
Everything works great in storage/app/public directories but i dont want it, i want my directory like storage/app/products/{id}/
at first look at my code:
filesystem.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
'productions' => [
'driver' => 'local',
'root' => storage_path('app/productions'),
'visibility' => 'private',
],
i create new array 'productions'
ProductionController.php
public function file()
{
return '<img src="'.Storage::disk('productions')->url('7/2.png').'">';
}
web.php (Route)
Route::group([
'middleware'=>'roles',
'roles'=>['Administrator','Prefabrykacja','Dyrektor Prefabrykacji']
], function () {
Route::get('/Produkcja',[
'uses'=>'ProductionController#index',
'as'=>'production.index']);
Route::post('/Produkcja/create',[
'uses'=>'ProductionController#create',
'as'=>'production.create']);
Route::get('/Produkcja/file',[
'uses'=>'ProductionController#file',
'as'=>'production.file']);
});
if i return
return '<img src="'.Storage::disk('productions')->url('7/2.png').'">';
Or
return '<img src="'.Storage::disk('local')->url('7/2.png').'">';
result is the same. both line return the image from storage/app/public/7/2.png will display not image storage/app/products/7/2.png
how can i display the image from my products folder and restrict resources to specifed role?
Regards

First of all, you're probably missing a symlink like this one for public (Local URL Host Customization). Also, you need to specify url parameter like for public and change visibility to public in order to allow others to see your files.
filesystem.php:
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
'productions' => [
'driver' => 'local',
'root' => storage_path('app/productions'),
'url' => env('APP_URL') . '/productions', // added line (directory within "public")
'visibility' => 'public', // modified visibility
],
],
Also make sure that you create a symlink at public/productions which will point to storage/app/productions directory. You can do this by using following command (for example):
cd LARAVEL_PATH && ln -s storage/app/productions public/productions

Related

Facing 403 error while uploading images on my sites dashboard

I am having issue whenever I upload image from my site's admin dashboard.
It's always 403 error.
I have all my possible best but no way.
This is the code in the filesystem.php. Please help, what am I not doing correctly?
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
'private' => [
'driver' => 'local',
'root' => storage_path('app/private'),
],
//---
// Used for Admin -> Log
'storage' => [
'driver' => 'local',
'root' => storage_path(),
],
// Used for Admin -> Backup
'backups' => [
'driver' => 'local',
'root' => storage_path('backups'), // that's where your backups are stored by default: storage/backups
],
I think CSRF is missing in this form.

calling asset on production env didn't work Laravel 8 (shared hosting)

hello there I'm having trouble with calling my asset in the production environment this is how the structure of the folder
Laravel/
storage
...
public_html/
storage(a symlink that I created and working fine using, ln -sfn
domains/sites.com/new/storage/app/public/posted-images/ ../public_html/)
...
and this is how I call the asset :
<img src="{{ asset('storage/'.$posts->image) }}" alt="" class="img-fluid">
this is how my filesystem.php looks a like :
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
'links' => [
public_path('storage') => storage_path('app/public'),
],
and in .env I put :
FILESYSTEM_DRIVER = public
So I found the solution like this :
first I'm deleting the storage link that I created with ln -sfn domains/sites.com/new/storage/app/public/posted-images/ ../public_html/
then, in filesystem.php change :
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
to
'public' => [
'driver' => 'local',
'root' => 'storage',
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
after that, make a request to store function and it will create the storage folder in public_html. Your old file maybe not gonna appear there so(file before you change the root to 'storage'), you can copy the data that not there from storage folder in the domain.
And the way i call the asset is the same as above.

Laravel Storage::disk('public_path')->files() returning wrong path

In the following route, inspecting the image paths that this method returns, they all have "/categories/" at the start but this is not right, the actual path should be /public/images/categories/gallery/1 not /public/categories/images/categories/gallery/1
Route::get('/categories/{group:id}', function (Group $group) {
$images = Storage::disk('public_path')->files('images/categories/gallery/' . $group->id);
return view('categories.index', ['group' => $group], compact('images'));
})->name('categories');
The following is an example of a route where it actually gets the correct paths
Route::get('/news', function () {
$categories = Category::get();
$images = Storage::disk('public_path')->files('images/news/gallery');
return view('news', ['categories' => $categories], compact('images'));
})->name('news');
So my question is, why is it in the previous route, it returns the incorrect path with /categories/ attached to the start but not in the one above, also how do I get the correct path in the first code example posted.
I tried changing the route to Route::get('/{group:id}', function ... etc and it works but I won't be able to use that because the url subdir will just be a number.
I also tried adding a forward slash to the path in the files() method so it was ->files('/images/categories/gallery/1'); but it still appends /categories/ to the start.
This is the config for the method in filesystems.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
'public_path' => [
'driver' => 'local',
'root' => public_path(''),
],
'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'),
],
],
The problem was not in my php but in the vue component I was using which failed to place a forward slash at the start of the src path so I changed
<source media="(min-width: 768px)" :srcset="`${image}`">
to
<source media="(min-width: 768px)" :srcset="`/${image}`">
and it works.

Laravel admin image cant access via url

I have create an app that need to upload image.
I use http://laravel-admin.org/
already setup all and follow the documentation. All work, but my image cannot access via url
its said 404 - Not Found
when access via url.
checked on explorer its already there.
here my config/filesystems.php
....
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
'admin' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'visibility' => 'public',
'url' => env('APP_URL').'/storage',
],
....
here my config/admin.php
....
'upload' => [
'disk' => 'admin',
'directory' => [
'image' => 'images',
'file' => 'files',
],
'host' => 'http://localhost:8000/upload/',
],
....
any can help and explain, got stuck about 3 hours :')
ref your explorer screen, so the image is stored in public disk/images
the easy way is access it by Storage::disk('public')->url('images/'. $image);
if you want create another disk, i can post more code
also you can use Accessor, in your model add this
public function getShowImageAttribute()
{
return \Storage::disk('public')->url('images/'. $this->attributes['image_column_name_here']);
}
so you can access it in the blade like this
{{ $user->show_image }}

Laravel add two directories to local disk

Filesystem.php
How can i add another directory to local disk.
Currently only having the images directory
'disks' => [
'local' => [
'driver' => 'local',
'root' => public_path('images/'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
You can only specify the root of each disk defined in config/filesystems.php. You are free to define as many disks as needed however:
'images-subdir' => [
'driver' => 'local',
'root' => public_path('images/subdir'),
],
If I understand your question correct, you need:
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
]
Source: https://github.com/laravel/laravel/blob/master/config/filesystems.php

Categories