eggplantSword's avatar

Dynamically create a folder in a public storage disk

I'm trying to create a new folder inside a public disk but I'm having issues. This is what I have so far

 while (Storage::disk('report_upload')->exists($name . '.xlsx')) {
            $name .= $counter;
            $counter++;

            Storage::disk('report_upload')->makeDirectory('Reportes-' . $request->request_time);
        }

        Excel::store(new SurveyReport($items), $name . '.xlsx', 
            'report_upload/Reportes-' . $request->request_time);

This is for a Job but they all fail with this error

InvalidArgumentException: Disk [report_upload/Reportes-2021-04-16 13:13:45] does not have a configured driver.

This is my config filesystems.php

  'report_upload' => [
            'driver' => 'local',
            'root' => public_path() . '/reports',
        ],

What am I doing wrong?

Also side question, this is to download all the files that get created, should I just take everything to a zip file regardless of what gets created and then delete everything? I wanted to create a folder for each time these files have to get created but that's where I'm stuck.

A folder does get created but not by me for the same file every time and I don't know what's up with that.

0 likes
19 replies
Wakanda's avatar

@msslgomez try

'report_upload' => [
            'driver' => 'local',
            'root' => public_path('reports') ,
        ],

and run php artisan config:clear

eggplantSword's avatar

ok, now I don't failed jobs BUT also nothing happens, no folder and no Excel files anywhere

Snapey's avatar

you are using $request->request_time but this includes characters that are not valid for use in a folder name.

eggplantSword's avatar

What can I replace it with? Which characters are permitted? I'm ok with keeping only the date

eggplantSword's avatar

I changed it to report_upload/reports_16_04_2021 and still nothing happens

Wakanda's avatar

@msslgomez

 while (Storage::disk('report_upload')->exists($name . '.xlsx')) {
            $name = $counter;
            $counter++;

            Storage::disk('report_upload')->makeDirectory('Reportes-' . uniqid());
        }

        Excel::store(new SurveyReport($items), $name . '.xlsx', 
            'report_upload/Reportes-' . uniqid());
eggplantSword's avatar

Still the same error: Disk does not have a configured driver

Snapey's avatar

open tinker, type

>>> config('filesystems')

and then paste the result

Snapey's avatar

@wakanda

this won't work, each time you call uniqid it will return a different value

 while (Storage::disk('report_upload')->exists($name . '.xlsx')) {
            $name .= $counter;
            $counter++;

        $newdir = 'Reportes-' . uniqid();

            Storage::disk('report_upload')->makeDirectory($newdir);
        }

        Excel::store(new SurveyReport($items), $name . '.xlsx',  'report_upload/'. $newdir);
eggplantSword's avatar

@snapey I can create the directory like this

        $path = public_path('report_upload/reports_' . date_format($request_date, 'd_m_Y'));
        $ex_path = 'report_upload/reports_' . date_format($request_date, 'd_m_Y');
        if (!File::exists($path)) {
           File::makeDirectory($path, 0755, true);
        }

        Excel::store(new SurveyReport($items), $name . '.xlsx', $ex_path);

And this works the folder gets created but the Excel file gives the error about the disk not having a configured driver.

Is there a way to create it and save it to a path itself instead of a Storage disk?

Snapey's avatar

you probably need to give it the full file path since it probably does not have a clue where report_upload might be

eggplantSword's avatar

I think either at this point I'm retarded or this is harder that it should be (I'm probably just overthinking it), I decided to create the Excel file using the disk. Then move it to the folder, but I can't figure it out, nothing is working.

Excel::store(new SurveyReport($items), $name . '.xlsx', 'report_upload');

if (Storage::disk('report_upload')->exists($name . '.xlsx')) {

// I've tried this
    Storage::disk('report_upload')->move(Storage::get($name . '.xlsx'),
            'reports/reports_' . date_format($request_date, 'd_m_Y') . '/' . $name . '.xlsx');

    Storage::move(Storage::get($name . '.xlsx'),
            'reports/reports_' . date_format($request_date, 'd_m_Y') . '/' . $name . '.xlsx');

    Storage::move(Storage::get('reports/' . $name . '.xlsx'),
            'reports/reports_' . date_format($request_date, 'd_m_Y') . '/' . $name . '.xlsx');
 }

The files are in on my local computer

C:\xampp\htdocs\unadeca\public\reports

Every way I've tried returns

File not found at path

Snapey's avatar

I can't follow what you are trying to do. You have a DISK called 'report_upload' ?

You never did show the config('filesystems')

Snapey's avatar

The files are in on my local computer

C:\xampp\htdocs\unadeca\public\reports

well thats where your code says put the file?

Excel::store(new SurveyReport($items), $name . '.xlsx', 'report_upload');

The third parameter is the DISK name. You cannot append further folder information to the disk name

You should add the subfolder name to the filename.

eggplantSword's avatar
eggplantSword
OP
Best Answer
Level 9

sorry for the confusion, the tinker command didn't work. But yea the filesystems.php looks like this

'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],

        'report_upload' => [
            'driver' => 'local',
            'root' => public_path('reports'),
        ],

        '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'),
        ],

    ],

I have it working as I want with the following code

        $request_date = date_create(JobRequest::find($this->request_id)->request_time);

        $path = public_path('reports/reports_' . date_format($request_date, 'd_m_Y'));
        if (!File::exists($path)) {
            File::makeDirectory($path, 0755, true);
        }

        Excel::store(new SurveyReport($items), $name . '.xlsx', 'report_upload');

        if (Storage::disk('report_upload')->exists($name . '.xlsx')) {
            rename(public_path('reports/' . $name . '.xlsx'),
                public_path('reports/reports_' . date_format($request_date, 'd_m_Y') . '/' . $name . '.xlsx'));
        }

Thank you for all your help.

Please or to participate in this conversation.