The Laravel documentation states that th FlySystem config is located in config/filesystems.php. Is there a way I can change where this file is loaded or can i state when using a disk to load from a different config?
So instead of
Storage::disk('local')->put('file.txt', 'Contents');
Something like
Storage::disk('myconfig::local')->put('file.txt', 'Contents');
It Supported Drivers: "local", "ftp", "s3", "rackspace";
Maybe you need take a look this docs
src/Illuminate/Filesystem/FilesystemManager.php#L70
/**
* Get the filesystem connection configuration.
*
* #param string $name
* #return array
*/
protected function getConfig($name)
{
return $this->app['config']["filesystems.disks.{$name}"];
}
'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_KEY'),
'secret' => env('AWS_SECRET'),
'region' => env('AWS_REGION'),
'bucket' => env('AWS_BUCKET'),
],
]
Never even occurred to me that I can simply use
Config::set('filesystems.disks', $config);
This allows me to set the config from anywhere in the code I choose. In my case I used the service provider in my package and a separate config file.
Related
when I try to visualise my picture with this basic url on google :
domain.com/storage/transports/picture.png
I get (404) not found.
(I don't have this error for a file outside the storage symlink in my tree picture and I can totaly see it without any problems, exemple :
domain.com/logo.svg
I have used php artisan storage:link to create the symlink, so far I'm abble to store the pictures exactly where I want to with that controller :
public function storeTransports(Request $request)
{
try{
session(['content'=>'content_transports']);
$request->validate([
'name' => [ 'string', 'max:255'],
'picture' => [ 'required','image','mimes:jpeg,png,jpg,gif,svg', 'max:2048']
]);
if(Transport::where('name', $request->name)->first()){
return redirect(RouteServiceProvider::HOME)->with('error_transports', 'Transport already exists');
}
else{
if($request->hasFile('picture')){
$sanitized_image_name = strtolower(preg_replace("([^A-Za-z0-9])", "", $request->name)).'_'.time();
$image_name = $sanitized_image_name.'.'.$request->picture->extension();
Transport::create([
'name' => $request->name,
'path_picture' => $image_name
]);
$request->picture->storeAs('public/transports',$image_name);
}
}
return redirect(RouteServiceProvider::HOME)->with('success_transports', 'Transport saved successfully');
} catch (\Illuminate\Database\QueryException $e){
return redirect(RouteServiceProvider::HOME)->with('error_transports',$e->errorInfo);
}
}
I can also visualize my pictures in my public folder :
i've rm rf public/storage many times already
here is my configfiles.php :
<?php
return [
'default' => env('FILESYSTEM_DISK', 'public'),
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
'throw' => false,
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
'throw' => false,
],
'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),
'throw' => false,
],
],
'links' => [
public_path('storage') => storage_path('app/public'),
],
];
I appreciate your help, I've been looking at all the stackoverflow talking about that for the past hour.
PS: I don't use vagrant.
EDIT:
i finally fixed my issue, as Matty10209 mentoned here,
"[...]When running "php artisan storage:link", Laravel creates an absolute path for the image to your local directory. Docker does not like this and wants a relative one, so it breaks the image link and you get a 404. You have to create the symlink manually[...]"
"[...] Go to docker and the CLI of your project container (should be at the bottom). Delete the current symlink you have created with Laravel, then navigate to the path you wish to create a new one. and run
ln -s ../storage/app/public storage
[...]"
I'm trying to create a Dashboard for a personal project,
I want the storage of the Dashboard to be completely separate from the front end,
I create a new disk in the filesystem.php and this is the details:
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
'backend' => [
'driver' => 'local',
'root' => storage_path('app/backend'),
'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'),
],
],
after that, I try to store some avatars and it works perfectly,
my problem is when I want to access the files from the view side,
Note that I try the visibility on both public and private
,also
I want to store sensitive files inside it
so I try:
url('/storage/avatars/filename.png'),
storage_path('backend/avatars/filename.png'),
storage::disk('backend')->get('avatars/filename.png'),
but nothing.
What is your question exactly? I assume you are trying to display images on a webpage, but they are not showing up?
This has to do with the visibility of your files. Only files in the storage/app/public directory are available for display. So images that should be visible using a URL, should be stored in that directory.
The best practice here is to upload the image to the Public disk. Make sure to create the symoblic link using php artisan storage:link.
For any other non-public file, you can set-up a Controller to return a download response
Another option is to display content right away in the browser, like a PDF. In that case, you should set the correct Content-Type while returning the response, with the contents of the PDF.
If this did not answer your question, please let me know.
On Laravel 8
file_get_contents(asset('storage/list.json');
Give me:
ErrorException
file_get_contents(http://localhost:8000/storage/list.json): failed to
open stream: HTTP request failed!
But: http://localhost:8000/storage/list.json exist and it is accesible via browser for example.
On my config/filesystem.php iv'e:
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
'permissions' => [
'file' => [
'public' => 0664,
'private' => 0600,
],
'dir' => [
'public' => 0775,
'private' => 0700,
],
],
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
And ive created the symbolic link with:
php artisan storage:link
command.
I've tried even to add manually file perssion to storage folder as described in the filesystem.php but nothing change.
You should use absolute paths when you want to work with files. file_get_contents() is fine. There are some file wrappers available in laravel but internally they all use file_get_contents().
Try
file_get_contents(public_path(asset('storage/list.json')));
If getting from storage file try
file_get_contents(storage_path('app/assets/list.json'));
See https://laravel.com/docs/5.5/helpers#method-resource-path
I'm using image field in laravel backpack 4.0 and it uploads the images without any problems. When I delete the image by using the delete button, it deletes the register (E.N. Probably means "it deletes the image from the database"), but not the image file from my local folder. I've checked the answer from backpack for laravel deleting image , but it did not help to fix my issue.
My config/filesystem:
'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'),
],
],
My Model code:
public function setImageAttribute($value)
{
$attribute_name = "image";
$disk = config('backpack.base.root_disk_name'); // or use your own disk, defined in config/filesystems.php
$destination_path = env('FOLDER_PUBLIC')."/uploads/medias"; // path relative to the disk above
// if the image was erased
if ($value==null) {
// delete the image from disk
\Storage::disk($disk)->delete($this->{$attribute_name});
// set null in the database column
$this->attributes[$attribute_name] = null;
}
// if a base64 was sent, store it in the db
if (starts_with($value, 'data:image'))
{
// 0. Make the image
$image = \Image::make($value)->encode('jpg', 90);
// 1. Generate a filename.
$filename = rand ( 10000 , 99999 ).'-'.strtolower(trim(preg_replace('/[\s-]+/', '-', preg_replace('/[^A-Za-z0-9-]+/', '-', preg_replace('/[&]/', 'and', preg_replace('/[\']/', '', iconv('UTF-8', 'ASCII//TRANSLIT', $this->title))))), '-')).'.jpg';
// 2. Store the image on disk.
\Storage::disk($disk)->put($destination_path.'/'.$filename, $image->stream());
// 3. Save the public path to the database
// but first, remove "public/" from the path, since we're pointing to it from the root folder
// that way, what gets saved in the database is the user-accesible URL
$public_destination_path = Str::replaceFirst(env('FOLDER_PUBLIC').'/', '', $destination_path);
$this->attributes[$attribute_name] = $public_destination_path.'/'.$filename;
}
}
public static function boot()
{
parent::boot();
static::deleting(function($obj) {
\Storage::disk('public')->delete($obj->image);
});
}
I have tried to change:
\Storage::disk('public')->delete($obj->image);
With:
\Storage::disk(config('backpack.base.root_disk_name'))->delete($obj->image);
But it is not working either,
Can anyone help me?
Sorry for my english
You're on the right track. Looks like something is misconfigured if code in boot() doesn't affect any changes. My guess is that that the $obj->image path to the file you're trying to delete is NOT the exact path to the file. You might need to add the destination folder there too.
public static function boot()
{
parent::boot();
static::deleting(function($obj) {
$disk = config('backpack.base.root_disk_name');
$destination_path = env('FOLDER_PUBLIC')."/uploads/medias";
\Storage::disk($disk)->delete($destination_path.'/'.$this->image);
});
}
If this works, to further improve the code, I recommend you turn the $disk and $destination_path variables into properties on the PHP class itself (the Laravel model). That way, you only define them in one place, and you can then use them in both methods, with something like $this->imageDisk and $this->imagePath.
Thanks for your reply, At the end I have fixed it, making this change:
public static function boot()
{
parent::boot();
static::deleting(function($obj) {
\Storage::disk('uploads')->delete($obj->image);
});
}
And in config/filesystem:
'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'),
],
'uploads' => [
'driver' => 'local',
/* 'root' => base_path().'/web/storage',*/
'root' => base_path().'/'.env('FOLDER_PUBLIC'),
],
],
Regards
I'm trying to store a file in the public folder storage/app/public/ but for some reason Laravel just seems to put it in the private storage/app/ folder.
If I understand correctly I'm supposed to just set the visibility to 'public' but that doesn't seem to change anything:
Storage::put($fileName, file_get_contents($file), 'public');
When I call getVisibility I get public so that seems to work fine:
Storage::getVisibility($fileName); // public
These are the settings in my 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',
],
's3' => [
'driver' => 's3',
'key' => env('AWS_KEY'),
'secret' => env('AWS_SECRET'),
'region' => env('AWS_REGION'),
'bucket' => env('AWS_BUCKET'),
],
],
When you call Storage::put, Laravel will use the default disk which is 'local'.
The local disk stores files at its root: storage_path('app'). The visibility has nothing with where the file should be stored.
You need to choose the public disk which will store the files at its root: storage_path('app/public'),
To do that, you need to tell Laravel which disk to use when uploading the file. Basically change your code to this:
Storage::disk('public')->put($fileName, file_get_contents($file), 'public');