Even PHP has "bugs" with IE

This is not a real mistake, but it is certainly not what you expect. I have sample code for uploading images:

if($type=="image/jpg" || $type=="image/jpeg" || $type=="image/pjpeg" || $type=="image/tiff" || $type=="image/gif" || $type=="image/png") {
   // make upload
else echo "Incorrect format ...."; 

The problem is that if I change the image extension, say to .jpgq or even .jpg%, and I try to load it, FF and Chrome will say that the file type is "application / octet stream", and usually the condition will be false.

IE, on the other hand, will say that the file type is "image / jpeg", the condition will be true, and the file will be downloaded. Of course, any browser trying to read the image later will not be able to do this.

This is not an error, because msdn.microsoft.com says: "If the" proposed "(provided by the server) MIME type is unknown (unknown and not ambiguous), FindMimeFromData immediately returns this MIME type" and "If the MIME type provided by the server is either known or ambiguous, the buffer is scanned in an attempt to verify or obtain the MIME type from the actual contents. "

My questions:

  • Why does IE / server know the actual MIME type at boot, but it cannot read it from the server?
  • How can I get around this problem (if the file does not have the correct extension, the condition must be false)? Is it possible to check the extension format (and not the MIME type)?
  • - any of the above extensions not recommended for use? Should I add others?
+3
2
+4

- mimetype, .

mimetype, , mimetype, , - . , mimetype .

,

/* verify and sanitize any file extension from mimetype
 */
    switch($subtype) {
    case 'pjpeg':
    case 'jpeg':
        if (!preg_match('/\.jp(e)?g$/i', $real_name)) {
            $real_name .= '.jpg';
        }
        break;

    default:
        if (!preg_match('/\.'.$subtype.'$/i', $real_name)) {
            $real_name .= ".$subtype";
        }
        break;
    }
0

Source: https://habr.com/ru/post/1737618/


All Articles