jrean's avatar

S3 file upload

Hi,

I try to understand how can I manage to upload mp3 files to S3. This is for a small podcast application.

I have the following code

$filesystem->disk('s3')->put($podcast->filename, $request->file('podcast')->getClientOriginalName());

When I try with a basic .txt file or even an image it works.

But as soon as I try to upload a mp3 file the code run then after a few second it returns and redirects (normal behaviour).

When I check on my S3 console the file is correctly upload except it is a 26 bytes file instead of 3.3 mo...

What did I miss? I googled a lot but didn't find a solution.

Thank you guys.

0 likes
5 replies
phildawson's avatar
Level 26

The second param is the data to be "put" not the $request->file('podcast')->getClientOriginalName() so you'll need something like file_get_contents around the path.

The 26 bytes is the original name saved inside a .mp3 file on s3 :)

Storage::disk('s3')->put('file.txt', 'Contents');
1 like
jrean's avatar

Thank you @phildawson but how do I put the content as you said? I put $request->file('podcast') as second parameter? I think I have already tried that and it didn't work.

Please provide a sample. Thank you for your light.

jrean's avatar

Ok I did:

$filesystem->disk('s3')->put($podcast->filename, file_get_contents($request->file('podcast')->getRealPath()));

It worked.

Is it the right way?

1 like

Please or to participate in this conversation.