PHP Laravel read files from another drive - php

I'm currently in the learning curve of Laravel.
I was wondering if there is a way using the Storage:: in Laravel to access files from another harddrive (using Windows).
Example, I've got Xampp with Laravel setup on drive C:, but I want to access files on E: outside of website directory. I've tried using Storage::files('E:') and File::files('E:') but that clearly doesn't work.
Thanks in advance.

Not tested this myself, but from the documentation page, i take it you need to edit the filesystem.php config file to something like:
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'partitionE' => [
'driver' => 'local',
'root' => 'E:/', // not really sure about this
]
// the rest of it
];
And then accessing files like
Storage::disk('partitionE')->put('file.txt', 'Contents');
If all this fails, you could go with a symlink and create a link to your E: inside your project folder.

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.

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.

Laravel filesystem outside project

Can I use laravel filesystem to save file outside current project?
I have this structure: project and cdn.
I've tried custom driver but fails. It wont put any file at cdn folder.
'custom' => [
'driver' => 'local',
'root' => '~/Code/cdn.web/storage',
],
$store = Storage::disk('custom')->put('index.txt', 'contents');
I've followed this answer too Storing files outside the Laravel 5 Root Folder
But there is no custom driver.
Is there anyway to make it working?
Thanks
Found the answer.
Laravel doesn't support ~ on the path.
I need to put full path to make it working.
'custom' => [
'driver' => 'local',
'root' => '/home/vagrant/Code/cdn.web/storage',
],

Categories