Welcome Guest, Not a member yet? Register   Sign In
Getting files mime types - Mission impossible ?

So, how come there's no reliable native way in php to get the mime type of a file ?
I am looking for a solution for days .
What i can see so far :
--> Fileinfo - the php extension . Is not available in most of the hosting services, therefore is not reliable for an application that needs to be moved from server to server .
--> get_mime_type() - same shit, it's old , deprectaed, and most of the hosting servers don't have it .

Sure, for images/flash we can use getimagesize() but for the other types ?
One would say to use exec('file -bi '.$file_name); but how many hosting services have functions like exec/system/shellexec enabled ? You can have it only if you have a dedicated/vps server but is not worth the risk.

I end up using cURL to get the mime type of a file (how lame is that???)
To be more detailed :
   $file_info = new finfo(FILEINFO_MIME);
   $this->file_type = $file_info->buffer(file_get_contents($this->upload_path.$this->file_name));
   $this->file_type = mime_content_type($this->upload_path.$this->file_name);
   $url = str_replace(FCPATH,base_url(),$this->upload_path);
   $url.= $this->file_name ;
   $ch = curl_init($url);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
   $content = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
   $this->file_type = current(explode(';',$content));

This works fine enough and is reliable, but really, do we have to end with using curl to get the mime type of a file ???

Do you have other reliable ways of doing this ?

Oh really ?
Nobody ? Nothing ?

$data = $this->upload->data();
echo $data['file_type'];




Edit: whoops, I post too quicly, also deprecated.

And even cURL is not available with all hosting services...

@noctrum. I know about that, don't worry, the problem is not of getting the mime type after i upload the file, rather than get it for every file i want . Basically i need to run some file checks from time to time (don't tell me to run a checksum) for various reasons (client decision).

@waldmaister. Yes i know, but is more available than finfo or mime_content_type() and it seems to be the only way you can do it in php at this moment without installing extensions .

So, what do we do ?

Theme © iAndrew 2016 - Forum software by © MyBB