Kirkland's avatar

Larvel Incorrectly Identifying audio/mpeg as application/octet-stream even with correct file attribute present

Hey there!

I see that it's been asked quite a few times before here and on SO, but I'm hard pressed to find an actual solution.

The Problem: When uploading a MP3 through Laravel (or apparently through the native PHP functions doing the same), the mime type is incorrectly identified as being a application/octet-stream type of file instead of the appropriate audio/mp3 type.

The apparent source of the problem: PHP's mime_content_type(...) function.

What I'm not able to find is a reliable solution to this problem. I was wondering how others have tackled this issue or if there was a Laravel specific work-around? Any ideas? Please help!

0 likes
5 replies
Kirkland's avatar

Ok, so I made an interesting discovery. When I dd($request->file('file')); I get the following output (with the correct mimeType data):

UploadedFile {#187 ▼ -test: false -originalName: "CUS2398472-20-AUG-2016-13-48-43.mp3" -mimeType: "audio/mpeg" -size: 47000471 -error: 0 path: "/private/var/folders/c7/6ws0lxy95dd_lhz1k067_zkc0000gn/T" filename: "phpkit5Rg" basename: "phpkit5Rg" pathname: "/private/var/folders/c7/6ws0lxy95dd_lhz1k067_zkc0000gn/T/phpkit5Rg" extension: "" realPath: "/private/var/folders/c7/6ws0lxy95dd_lhz1k067_zkc0000gn/T/phpkit5Rg" aTime: 2016-09-19 18:06:14 mTime: 2016-09-19 18:06:13 cTime: 2016-09-19 18:06:13 inode: 4251949 size: 47000471 perms: 0100600 owner: 501 group: 20 type: "file" writable: true readable: true executable: false file: true dir: false link: false }

But when I call the getMimeType() I get the "application/octet-stream" output. So the Laravel method is returning something different than what the uploaded file is providing. I've added "'audio/mpeg' => 'mp3'," to the array in MimeTypeExtensionGuesser.php. Any suggestions?

Kirkland's avatar

I've just proven that this is a Laravel issue and not a PHP/file issue by creating a vanilla PHP upload form and var_dumping $_FILES. This was the result (the correct results):

array(1) { ["fileToUpload"]=> array(5) { ["name"]=> string(15) "CUS12309821-20-AUG-2016-13-48-13.mp3" ["type"]=> string(10) "audio/mpeg" ["tmp_name"]=> string(66) "/private/var/folders/c7/6ws0lxy95dd_lhz1k067_zkc0000gn/T/ph‌​pf6cwMf" ["error"]=> int(0) ["size"]=> int(40340291) } }

I strongly believe that this is an issue with the guess() method called by the getMimeType() method. Could someone please weigh in on this?

julian2020's avatar

I know this topic is old, but right now I encountered a similar problem.

Uploading an image with a size less then ~ 1500 KB and everything's working properly:

  "logo" => UploadedFile {#606 ▼
    -test: false
    -originalName: "logo.jpg"
    -mimeType: "image/jpeg"
    -size: 1476245
    -error: 0
    #hashName: null
    path: "C:\xampp\tmp"
    filename: "php95EC.tmp"
    basename: "php95EC.tmp"
    pathname: "C:\xampp\tmp\php95EC.tmp"
    extension: "tmp"
    realPath: "C:\xampp\tmp\php95EC.tmp"
    aTime: 2018-04-01 15:52:34
    mTime: 2018-04-01 15:52:34
    cTime: 2018-04-01 15:52:34
    inode: 0
    size: 1476245
    perms: 0100666
    owner: 0
    group: 0
    type: "file"
    writable: true
    readable: true
    executable: false
    file: true
    dir: false
    link: false
    linkTarget: "C:\xampp\tmp\php95EC.tmp"
}

But then if I try to upload a bigger file (e.g. 2 MB), the mime type changes to the octet-stream and dd contains nearly no information:

  "logo" => UploadedFile {#606 ▼
    -test: false
    -originalName: "logo_big.jpg"
    -mimeType: "application/octet-stream"
    -size: 0
    -error: 1
    #hashName: null
    path: ""
    filename: ""
    basename: ""
    pathname: ""
    extension: ""
    realPath: "C:\xampp\htdocs\logo_uploader\public"
    aTime: 1970-01-01 01:00:00
    mTime: 1970-01-01 01:00:00
    cTime: 1970-01-01 01:00:00
    inode: false
    size: false
    perms: 00
    owner: false
    group: false
    type: false
    writable: false
    readable: false
    executable: false
    file: false
    dir: false
    link: false
  }

The same applies with PHP's $_FILES, so in my mind this has nothing to do especially with Laravel. My post_max_size is 8M, so this should not be the problem.

I think there is an additional limitation of the filesize anywhere, but don't know where. Does anyone have an idea?

Arcas1's avatar

@julian2020 Hello Julian, I have the same problem and it is indeed a problem with the php.ini configuration, the best way to correct it is to modify it and increase the memory in these two fields:

post_max_size = 30M upload_max_filesize = 30M

The place to update should be obtained directly from phpinfo(); Well, if you don't get it from there and use a command like:

php --ini | grep Loaded

It will return us:

Loaded Configuration File: /etc/php/7.4/cli/php.ini

And the php.ini that we want to modify is the apache one:

/etc/php/7.4/apache2/php.ini

Once that is done the upload of documents should work

3 likes
CharlesKyalo's avatar

Hey, I have a similar problem with video uploads. For images use the intervention package and it should work fine.

Please or to participate in this conversation.