cameronscott137's avatar

Customizing the name of an uploaded .zip file

My app accepts user-submitted .zip files, which I temporarily store in local storage:

Storage::disk('local')->put('exports', $request->custom_report_url);

The challenge is that this creates a file with what I assume is some kind of hash:

'app/storage/exports/U44SEhM1wycNgMU0bV6qieSRiGJwMPvkDpBDgkyo.zip'

This makes that zip file a pain to find later in the process.

Is there a way that I can define a custom name for that zip file? Ideally, I'd like to do something like:

Storage::disk('local')->put('exports/export_' . $export-id . '.zip', $request->custom_report_url);

However, when I run that code, it creates the following filepath:

'app/storage/exports/export_1.zip/U44SEhM1wycNgMU0bV6qieSRiGJwMPvkDpBDgkyo.zip'

Close, but not what I need. Any ideas?

0 likes
3 replies
Snapey's avatar

use putFileAs

Storage::putFileAs(
    'exports',$request->custom_report_url , 'export_' . $export-id . '.zip');
Cronix's avatar
Cronix
Best Answer
Level 67

Ah didn't notice you were using Storage and not uploads. It's almost the same through, look in the File Uploads > Specifying A File Name section

https://laravel.com/docs/5.5/filesystem#file-uploads

Of course, you may also use the putFileAs method on the Storage facade, which will perform the same file manipulation as the example above:

$path = Storage::putFileAs(
    'avatars', $request->file('avatar'), $request->user()->id
);

Please or to participate in this conversation.