Using with flash upload, finding the mime-type

See all posts Add a new post

Using with flash upload, finding the mime-type new!
by Vincent Morel, 16 years, 4 months ago
I'm using a flash uploader and the problem is that flash upload file with application/octet-stream for every kind of file! So if you want to use all the image stuff I find a small hack for your class... Here it is an tell me what do you think about it. Maybe good for a 0.26!! I test it on macosx, it works great (it use the shell). If none of the 3 function works, I don't know how to get the right mime-type... Here is my code, juste inserted before // if the file is an image, we gather some useful data
if (array_key_exists($this->file_src_mime, $this->image_supported))...
//Maybe the file come from flash so the mime-type is application/octet-stream even for an image
if($this->file_src_mime == 'application/octet-stream') {
  if (function_exists('mime_content_type')) { //Double check the mime-type with magic-mime if avaible
    $this->file_src_mime = mime_content_type($this->file_src_pathname);
  } elseif (strlen($mime=@shell_exec("file -bi ".escapeshellarg($this->file_src_pathname)))!=0) { //Using shell if unix an authorized
    $this->file_src_mime = trim($mime);
  } else { //Check if fileinfo extension is available
	if (!extension_loaded("fileinfo.so") && !dl("fileinfo.so")) {
	  return false;
	} else {
	  if (function_exists('finfo_open')) {
        $f = finfo_open(FILEINFO_MIME);
        $mime = finfo_file($f, $this->file_src_pathname);
        finfo_close($f);
        $this->file_src_mime = $mime;
      } elseif (class_exists('finfo')) {
        $f = new finfo( FILEINFO_MIME );
        $this->file_src_mime = $info->file($this->file_src_pathname);
      }
    }
  }
}
Reply
Re: Using with flash upload, finding the mime-type new!
by Vincent Morel, 16 years, 4 months ago
Maybe you can add a variable in the constructor '$flash - false' so the double check is done only if you set the flash to true.Reply
Re: Using with flash upload, finding the mime-type new!
by colin, 16 years, 4 months ago
Thank you for the code. I had a look at it, and will try to implement it. In fact, I will probably rework the MIME detection code as it needs quite a clean up, and I will use that opportunity to add your methods.

Does Flash rewrite the MIME type for all uploaded files? I wasn't aware of that.Reply
Re: Using with flash upload, finding the mime-type new!
by Vincent Morel, 16 years, 4 months ago
Yes, flash do that for every file it uploads. So you have to check the mime physically on the tmp file. I spent a lot of time on it... If you want some information, do not hesitate to send me a mail. I speak French (I leave in Québec).
Thanks.Reply
Re: Using with flash upload, finding the mime-type new!
by Leika, 11 years, 5 months ago
As noted in the post, PHP's advanced fnotcinus can determine type by content. Reproducing that would be tedious, expensive, and error-prone in pure PHP. The code provided here is to establish a baseline set of code that can be reliably distributed to unknown platforms without fear of catastrophic failure if the system does not support the fnotcinus provided by default builds or more recent versions of PHP. In other words, it is a compatibility method combined with a best-effort algorithm.In the case of this function running without support for the PHP fnotcinus for a filename with no matching extension, the last_resort method of the function will return application/octet-stream . While this may not be the desired outcome, it will still generate a valid generic MIME type.Your scenario of a filename without an extension shows a weakness in this approach; however, it should also be noted that a file of type JPEG with an extension of png will receive a MIME type of image/png rather than image/jpeg . This scenario is potentially worse than the one you brought up as the MIME type in this scenario is wrong whereas your scenario's result of the application/octet-stream MIME type is generic yet valid. Of course if a text file is called converting.gif.to.jpg , it would also improperly receive a MIME type of image/jpeg .For my applications, such failings are acceptable as MIME type recognition should always be considered suggestions as even the advanced PHP MIME fnotcinus have situations where they identify files properly. If this is unacceptable for your application, you either need to come up with a different solution or rethink your application's reliance on MIME type identification.Reply