Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ahmadbadpey's avatar

putFile method stores mp3 files as .bin in laravel

I want user could upload audio files to server for that I wrote this :

public function sendAudio (SendAudioFormRequest $request)
{
if ($request->hasFile('audio')) {

    $path =
        Storage::disk('user')->putFile(
            'audios', $request->file('audio')
        );

        dd($path);

}
}

But when I upload a .mp3 file, Unexpectedly I see that stored as a .bin file.

I do not know why this occurred? Does anyone has encountered this problem?

0 likes
4 replies
bencarter78@hotmail.com's avatar

I was finding the same issue but for a .docx file. I noticed that the $request->file('filename')->extension() method was incorrectly defining the filetype.

It actually says this in the docs

File Paths & Extensions ...This extension may be different from the extension that was supplied by the client

I got around this with the following...

$request->file('filename')->storeAs(
    $directory,
    $request->file('filename')->getClientOriginalName() . '.' . $request->file('filename')->getClientOriginalExtension()
);
2 likes
sam.martins's avatar

Thanks @bencarter78!

I had the same problem. This is my solution using methods more similar to those of @ahmadbadpey:

$file = new File($request->file);
       
// Generate unique name with real extension using the same method used by Storage::putFile()
$fileHash = str_replace('.' . $file->extension(), '', $file->hashName());
$fileName = $fileHash . '.' . $request->file('file')->getClientOriginalExtension();
        
$source = Storage::putFileAs('chat/attachments', $file, $fileName);
ahishamali's avatar

you can use the storeAs(PATH, NAME_YOU_WANT) method. you can add time() method before the name to make it unique. in the name parameter just use the $request->file('file_name')->getClientOriginalName() it will give the name with the extension

Mash Square's avatar

I wanted to upload a file type that is not in the usual list ('mp3', 'jpg' etc...) and getClientOriginalName() would return a .bin extension which in this case is obviously wrong.

My take on fixing this was using a PHP library function which reads the string name and finds everything after the "."

So in short to grab the extension you can do the following:

 $extension = substr($file->getClientOriginalName(), strpos($file->getClientOriginalName(), "."));

and then just attach it by the end of the filename string without the extension

Please or to participate in this conversation.