[eluser]Twisted1919[/eluser]
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 :
Code:
if(class_exists('finfo'))
{
$file_info = new finfo(FILEINFO_MIME);
$this->file_type = $file_info->buffer(file_get_contents($this->upload_path.$this->file_name));
}
elseif(function_exists('mime_content_type'))
{
$this->file_type = mime_content_type($this->upload_path.$this->file_name);
}
elseif(function_exists('curl_init'))
{
$url = str_replace(FCPATH,base_url(),$this->upload_path);
$url.= $this->file_name ;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_exec($ch);
$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 ?