Multi storage paths - laravel

I have server with 2 hdd. I want to use one for all media files, which are on storage/app/media for current media files and other for all files before 2018.
How can I switch storage path depends on query I make.
All media are saved on media table. I can make changes on table if needed.

In your config\filesystem.php update disk array and add your new disks like this:
'public_two' => [
'driver' => 'local',
'root' => storage_path('app/public/public_two'),
'url' => env('APP_URL').'/storage/public_two',
'visibility' => 'public',
],
and you call your diks like this: Storage::disk('public_two');
Please be free to read the FileSystem Documentation

Related

Why is the array empty on get allFiles with Laravel storage

I have an Laravel application witch works with an ftp server. Till now, I used the ftp service from hostpoint.ch. Everything works well.
Now, the customer like to use his own ftp server. So I created a new disk for the new ftp.
'ftp_customer' => [
'driver' => 'ftp',
'host' => 'xxx.xxx.xx.x',
'username' => 'xxx',
'password' => 'xxx',
'port' => 21,
'passive' => true,
'root'=> '/',
],
To test the server, I created the following script:
Storage::disk('ftp_customer')->put('file1.txt', 'Contents');
$files = Storage::disk('ftp_customer')->allFiles();
dd($files);
On the server, the file1.txt exists:
-rw-rw-rw- 1 ftp ftp 8 Dec 20 13:43 file1.txt
Therefore, I know, that the put method works.
But the get allFiles returns always an empty array.
I tried to create first a folder and use it. Also, I tried to change the config.
I'm not sure if it creates problems, because it's a Windows server.
Does anyone have an idea how to solve it?

Laravel Storage not storing created PDF files

I am facing an issue where my created PDF's are not being stored in laravel.
I have the following set in my filesystems.php under the disks tag
'completedforms' => [
'driver' => 'local',
'root' => storage_path('app/storage/completedforms'),
],
In my controller I have the following:
$pdf = PDF::loadView('report.form19', compact('generalreport','monthlyRoll', 'nightsInMonth', 'groupfee', 'subs', 'wing','weeksinmonth', 'meetingnights', 'lastRollMap', 'month_name', 'totalmember', 'totalcadets', 'totalnco', 'totalto', 'totalofficer'));
Storage::disk('completedforms')->put('Form 19 - '.$month_name . ' ' . $lastRollMap->roll_year.'.pdf', $pdf->output());
return $pdf->download ('Form 19 - '.$month_name . ' ' . $lastRollMap->roll_year.'.pdf');
I have added use Illuminate\Support\Facades\Storage; to my Controller
The pdf is created and downloads no issues, however the file is not being saved in the located as difined for the disk in the filesystems.php.
I have created the folder manually to ensure it exists. I have even set the file name to test.php to remove any variables fromm the file name
storage_path already starts at: /storage,
so change it like so:
'root' => storage_path('/desired'),

Laravel backup with spatie/laravel-backup - There is no disk set for the backup destination - Google Cloud Storage

I'm unable to backup to GCS from Laravel app
using spatie/laravel-backup
$ php artisan backup:run
Starting backup...
Dumping database reviewbooster...
Determining files to backup...
Zipping 720 files...
Created zip containing 720 files. Size is 29.86 MB
Copying zip to disk named gcs...
Copying zip failed because: There is no disk set for the backup destination.
Backup completed!
filesystems.php
'gcs' => [
'driver' => 'gcs',
'project_id' => env('GOOGLE_CLOUD_PROJECT_ID', 'project-id'),
'key_file' => env('GOOGLE_CLOUD_KEY_FILE', null), // optional: /path/to/service-account.json
'bucket' => env('GOOGLE_CLOUD_STORAGE_BUCKET', 'bucket'),
'path_prefix' => env('GOOGLE_CLOUD_STORAGE_PATH_PREFIX', null), // optional: /default/path/to/apply/in/bucket
'storage_api_uri' => env('GOOGLE_CLOUD_STORAGE_API_URI', null), // see: Public URLs below
'visibility' => 'private', // optional: public|private
],
backup.php
'destination' => [
/*
* The filename prefix used for the backup zip file.
*/
'filename_prefix' => '',
/*
* The disk names on which the backups will be stored.
*/
'disks' => [
'gcs',
],
],
I just had this same problem.
For me, the root cause was that my .env file had incorrect variables names that didn't match what Laravel expected in config/filesystems.php.
For example, I had:
AWS_KEY=xxx
AWS_SECRET=xxx
AWS_REGION=us-east-1
AWS_BUCKET=xxx
instead of:
AWS_ACCESS_KEY_ID=xxx
AWS_SECRET_ACCESS_KEY=xxx
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=xxx
P.S. Once I fixed that, I tried running the backup again and got a new error (403 Forbidden, Access Denied, Error executing "PutObject").
So then I visited https://console.aws.amazon.com/iam/home?region=us-east-1#/users/ and clicked "Add inline policy" for the relevant user. (See this answer.)

Laravel change filesystem disks path on run time

I am aware of the filesystems.php to create disks and I'm currently using it, having ~~ 20 disks configured.
I have a new problem with these, I'm currently trying to prefix to every disk, a string. The problem is that the paths are being saved when the php artisan config:cache is run but I need to change them on run time, as n example, for User Sergio it would need to append Sergio/ to the following disk for example:
//filesystems.php
'random' => [
'driver' => 'local',
'root' => storage_path('app/random'),
],
Then
Storage::disk("random")->getDriver()->getAdapter()->getPathPrefix();
//outputs /var/www/html/project/storage/app/random
and the goal is setting configurations in for example the middleware i'm currently setting the tentant database already like this
//Middleware
Config::set('database.connections.tenant.database', "Sergio");
DB::reconnect('tenant');
I can currently set the paths correctly with
Config::set('filesystems.disks.random.root',storage_path('app/Sergio/random'));
But i'm worried since that if before that line I try to reach to the path, the storage saves the initial path in memory instead of re-fetching it after it is altered.
For example. doing this without any middleware.
$fullPath1 = Storage::disk("random")->getDriver()->getAdapter()->getPathPrefix();
Config::set('filesystems.disks.random.root',storage_path('app/Sergio/random'));
$fullPath2 = Storage::disk("random")->getDriver()->getAdapter()->getPathPrefix();
What was intended to happen is that $fullPath1 would output the initial path which is /var/www/html/project/storage/app/random and then $fullPath2 would output /var/www/html/project/storage/app/Sergio/random
Is there any way of letting the Storage know that I've changed the disks local paths?
How about adding a new config instead of updating the already loaded one, something like this:
private function addNewDisk(string $diskName)
{
config(['filesystems.disk.' . $diskName => [
'driver' => 'local',
'root' => storage_path('app/' . $diskName),
]]);
}
and prior to using the Storage facade, call above method that way the config will be updated and when you use new disk, it will try to resolve again based on updated config.
{
....
$this->addNewDisk('new_random');
Storage::disk('new_random')->get('abc.txt'); // or any another method
...
}

Is there any way I can set timezone in laravel 4 database.php configuration file?

Is there any way I can set timezone in laravel 4 database.php configuration file (like 'timezone' => 'UTC')? I'm using mysql date, now and other functions which is giving me two separate time for production and development.
Thanks
Go to app->config->app.php in your laravel directory. On line 43 you can alter the default timezone to whatever you need.
Laravel 4 default:
'timezone' => 'UTC',
Your timezone:
'timezone' => 'America/Los_Angeles',
This change will then carry over to your database migrations. Also, if you need to set different timezones based on environment settings, simply add a directory in app->config directory that corresponds to the environment name you specified (ie - "production"). Place a duplicate of "app.php" into your newly created directory and alter the timezone setting on line 43 accordingly.
Yes, you can add the following at the top of database.php, before return array:
date_default_timezone_set('UTC');
If you want to New York:
date_default_timezone_set('America/New_york');
And so on. I have tested it and works fine to me.
None of currently available answers helped me, so I came up with that solution:
'mysql' => array(
...
'options' => [
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET time_zone = "+00:00"',
],
),
'options' should be added to mysql connection data (app/config/database.php).
Add 'timezone' => '+05:30' within 'mysql' in your config->database.php file
'mysql' => [
'driver' => 'mysql',
.....
.....
.....
'timezone' => '+05:30'
],

Resources