Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The file "" does not exist in MimeTypeGuesser.php:123 #23492

Closed
Mr-Anonymous opened this issue Mar 11, 2018 · 11 comments
Closed

The file "" does not exist in MimeTypeGuesser.php:123 #23492

Mr-Anonymous opened this issue Mar 11, 2018 · 11 comments

Comments

@Mr-Anonymous
Copy link

  • Laravel Version: 5.3.31
  • PHP Version: 7.0.12
  • Database Driver & Version: MySqli

Description:

I have Laravel Version 5.3 and sometimes when a user uploads a file, it fails with this message:

production.ERROR: Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException: The file "" does not exist in /home/app/vendor/symfony/http-foundation/File/MimeType/MimeTypeGuesser.php:123
Stack trace:

This error does not happen all the time and it only happens in some uploads. I notice these errors in my laravel logs.

The file is uploaded via Ajax (Angular) and the Laravel Controller that handles that POST, uses getMimeType() like this:

$postData = $request->only('file');
$file = $postData['file'];
if (empty($file)) {
       return response()->json(['error' => 'file not found.'], 400);
}
else {
       $mime = $file->getMimeType();
       ...
}

What is interesting is, the code does not fail in if (empty($file)) case if the file was really empty. But it only fails when it comes to $file->getMimeType() line with the above error message.

I have already searched the issues here and found couple of discussions like: #12350

I am still not sure what the solution here is. I don't want to upgrade Laravel to 5.6 yet and I dont want to edit the main application files inside laravel/framework/src/ unless there is no other choice. Can someone help me understand what maybe the cause for this issue and how can I resolve this in Laravel 5.3?

@emilianotisato
Copy link

Can you detect witch type of files are causing this Exception?

It's not a Laravel issue but a symfony/http-foundation related config issue.
Check this stackoverflow question:
https://stackoverflow.com/questions/44051722/laravel-file-download-php-fileinfo-extension-not-enabled

@Mr-Anonymous
Copy link
Author

Mr-Anonymous commented Mar 11, 2018

Hi @emilianotisato

Thanks for getting back to me on this. Unfortunately I can't detect what the files are since the log does not show the file name and I don't log the upload posts. But from what I have seen all the other uploads so far has been MOV files (since it is video uploads). So I am assuming it might be a mov file recorded from iPhone. Since it doesn't happen to all files I am not sure. I also tried changing getMimeType() to getClientMimeType() but the other method gives other issues where some mp4 or flv files are incorrectly shown as bin which is another issue altogether. So I am stumped on what to do next. Saw the SO link but since I don't know what the file types were and which user tried to upload it, I am not sure on the next step I can try. The log entry's Stack trace doesn't give much useful information to trace the origin.

Thanks,
Neel.

@Mr-Anonymous
Copy link
Author

Hi @emilianotisato

Do you think if I use a try block as a dirty trick around this issue, is it ok? I mean something like this:

try {
         $mime = $file->getMimeType();
       } catch (\Exception $e) {
         $mime = $file->getClientMimeType();
       }
... proceed with the rest ...

Its probably not addressing the core issue I guess. But is the above method worth trying or not recommended?

@devcircus
Copy link
Contributor

I've seen this behavior when a file size larger than allowed in php settings.

@emilianotisato
Copy link

Mmm.. Now in a second look, I am not sure about your first validation:

if (empty($file)) {
       return response()->json(['error' => 'file not found.'], 400);
}

Because in the symfony MimeTypeGuesser they ar validating is_file() before throwing the exception (check line 123 witch is same as your error log):
https://github.com/symfony/http-foundation/blob/3.1/File/MimeType/MimeTypeGuesser.php

So maybe you can replace your empty($file) for ! is_file($file) and handle an apropiarte response to the user asking to contact support referring witch file are failing...

@Mr-Anonymous
Copy link
Author

Hi @emilianotisato Thanks again. I actually added the if (empty($file)) condition to my controller after I noticed the The file "" does not exist error showing up in logs to see if that could resolve it before it goes to the getMimeType(). What you said makes sense and I will change it to is_file() validation instead.

Hi @devcircus I have 300MB as the max upload size and my frontend javascript shows an error if the file added is more than 300MB. However, now that you pointed it out, I think I might add a validation check before calling getMimeType() to check the max upload size. But then again, if it is due to max upload size, it shouldn't even be reaching the controller's line with getMimeType() in the first place, isn't it?

@devcircus
Copy link
Contributor

Yeah. Good point. It shouldn't get that far.

@Mr-Anonymous
Copy link
Author

Ok, here is an update on this and I have a feeling the issue is resolved.

@devcircus was spot on with this. In the end, it does appear to be an issue with the max upload limit perhaps. I am not sure why it happened in the first place because my frontend javascript checks the max file size of 300MB before posting the data and similarly, my php was already set to the same 300MB limit. After your support, I was gonna approach this in 2 steps:

  1. First I was gonna add a validation check in the controller as well for max upload size limit.

  2. And if the error continues, I was gonna add is_file() condition and pass the info to user asking to contact support with more details.

What I did:

Since the last post, I added this to my controller:

     // Check file size ========
     $validator = Validator::make($postData, [
         'file'  => 'required|max:307200',
     ]);

     if ($validator->fails()) {
        return response()->json(['sizeError' => $validator->errors()->getMessages()], 400);
     }

Next, although my php setting already has 300MB max upload size, I increased it to 400MB so there is some breathing space between the limit I set in laravel controller and the original php settings. Therefore, I am now letting Laravel filter the max size to only 300MB although my php server is now set to 400MB.

After the above 2 edits, I no longer see these error messages in the logs. So, I feel it might be safe to say the issue is resolved. I have been observing for couple of days and fingers crossed that it doesn't appear again.

Thanks to @emilianotisato and @devcircus for your support. Your kindness helped me in resolving this issue I was stuck on for couple of weeks. Much obliged indeed.

Cheers,
Neel.

@jctommasi
Copy link

I have this issue until 10 minutes ago when i read @devcircus and i realized that my file was too heavy.
before that i ran composer update and it update some php things and voilà !

@byustephen
Copy link

I had this same issue and it was the max upload limit on PHP. Look in your config file for a line that says
upload_max_filesize and set that to something sane like 20M or 256M. Also, be sure you are changing the right config file. If you run php -i in the command line you'll get your command line config file. You need to run phpinfo() from a browser window and then check in there for your loaded configuration path. Mine looks like Loaded Configuration File | /etc/php/7.2/fpm/php.ini

Then restart your php server sudo service php7.2-fpm restart and it should work.

@jonathandey
Copy link

I thought I should chime in becuase I have just resolved this for myself.

In my case I had everything already configured correctly as per the solutions above, which really got me scratching my head. It turns out I just didn't have ext-imagick installed for my PHP version...

It works now! I hope this can help someone else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants