FileDrop icon indicating copy to clipboard operation
FileDrop copied to clipboard

file.type seems doesn't work for several files

Open trinitrotoluene76 opened this issue 8 years ago • 8 comments

Hi, i'm testing filedrop and I have notice that file type function doesn't work for several files:

  • new file.txt (empty)
  • *.md, *.markdown, *.mkdown...
  • *.7z, *.bz2, *.rar

the php function mime_content_type() retrieves those files.

trinitrotoluene76 avatar Aug 31 '17 11:08 trinitrotoluene76

What do you mean exactly? I don't understand you.

ProgerXP avatar Aug 31 '17 14:08 ProgerXP

in basic.html, if i try alert('file type is: '+file.type); in files.each(function (file) {...} the alert box contains "file type is: " when i upload the README.md for example, instead "file type is: text/markdown". For most of file extensions it's ok, but not for these quoted.

trinitrotoluene76 avatar Aug 31 '17 16:08 trinitrotoluene76

The type comes from the browser which in turn detects it based on file extension. It's usually bad practice to rely on this property since it's not portable and depends on browser version.

ProgerXP avatar Aug 31 '17 17:08 ProgerXP

I tested with Chrome 60.0.3112.101 (Build officiel) (64 bits). What is the best pratice to detect mime type before opload?

trinitrotoluene76 avatar Aug 31 '17 17:08 trinitrotoluene76

type depends on file extension so it's trivial to fake. You have two options:

  1. Validate file data before upload by reading a few first bytes and comparing them with well-known signatures for file types you need (e.g. JPEG). Google for them, they're available.
  2. Validate it after upload using any of the plenty PHP functions.

ProgerXP avatar Aug 31 '17 17:08 ProgerXP

Thanks for reply. I knew that it's trivial to fake, but I thought put a first verification on the client side to avoid bad file by mistake and a second verification on the server side with your method for example.

trinitrotoluene76 avatar Aug 31 '17 18:08 trinitrotoluene76

Yes, that could be used but type is unreliable anyway because one browser might report one file type as something, another browser will report the same type as something else, or even two different versions of the same browser may work differently.

If you intend to validate common formats like images then you can definitely rely on type. If you want it for things like md or 7z then it's a bad idea and you better implement a simple signature check as I have described above (should not be hard with FileDrop).

ProgerXP avatar Aug 31 '17 18:08 ProgerXP

ok, cristal clear now, thanks

trinitrotoluene76 avatar Aug 31 '17 18:08 trinitrotoluene76