Re: [PHP] PHP to get File Type
There is a more elegant way: http://br2.php.net/manual/en/function.mime-content-type.php and the preferred way: http://br2.php.net/manual/en/function.finfo-file.php Angelo 2008/10/14 Aschwin Wesselius [EMAIL PROTECTED] Manoj Singh wrote: Hello All, Is there any function in PHP to get the file/Mime type of any file? Any help will be appreciated. Hi, There are better and more elegant ways to do this, but I'm not aware of them. Here is what I use most of the time. You only use the filename as $value for get_mimetype(). function get_file_extension($file) { return array_pop(explode('.',$file)); } function get_mimetype($value='') { $ct['htm'] = 'text/html'; $ct['html'] = 'text/html'; $ct['txt'] = 'text/plain'; $ct['asc'] = 'text/plain'; $ct['bmp'] = 'image/bmp'; $ct['gif'] = 'image/gif'; $ct['jpeg'] = 'image/jpeg'; $ct['jpg'] = 'image/jpeg'; $ct['jpe'] = 'image/jpeg'; $ct['png'] = 'image/png'; $ct['ico'] = 'image/vnd.microsoft.icon'; $ct['mpeg'] = 'video/mpeg'; $ct['mpg'] = 'video/mpeg'; $ct['mpe'] = 'video/mpeg'; $ct['qt'] = 'video/quicktime'; $ct['mov'] = 'video/quicktime'; $ct['avi'] = 'video/x-msvideo'; $ct['wmv'] = 'video/x-ms-wmv'; $ct['mp2'] = 'audio/mpeg'; $ct['mp3'] = 'audio/mpeg'; $ct['rm'] = 'audio/x-pn-realaudio'; $ct['ram'] = 'audio/x-pn-realaudio'; $ct['rpm'] = 'audio/x-pn-realaudio-plugin'; $ct['ra'] = 'audio/x-realaudio'; $ct['wav'] = 'audio/x-wav'; $ct['css'] = 'text/css'; $ct['zip'] = 'application/zip'; $ct['pdf'] = 'application/pdf'; $ct['doc'] = 'application/msword'; $ct['bin'] = 'application/octet-stream'; $ct['exe'] = 'application/octet-stream'; $ct['class']= 'application/octet-stream'; $ct['dll'] = 'application/octet-stream'; $ct['xls'] = 'application/vnd.ms-excel'; $ct['ppt'] = 'application/vnd.ms-powerpoint'; $ct['wbxml']= 'application/vnd.wap.wbxml'; $ct['wmlc'] = 'application/vnd.wap.wmlc'; $ct['wmlsc']= 'application/vnd.wap.wmlscriptc'; $ct['dvi'] = 'application/x-dvi'; $ct['spl'] = 'application/x-futuresplash'; $ct['gtar'] = 'application/x-gtar'; $ct['gzip'] = 'application/x-gzip'; $ct['js'] = 'application/x-javascript'; $ct['swf'] = 'application/x-shockwave-flash'; $ct['tar'] = 'application/x-tar'; $ct['xhtml']= 'application/xhtml+xml'; $ct['au'] = 'audio/basic'; $ct['snd'] = 'audio/basic'; $ct['midi'] = 'audio/midi'; $ct['mid'] = 'audio/midi'; $ct['m3u'] = 'audio/x-mpegurl'; $ct['tiff'] = 'image/tiff'; $ct['tif'] = 'image/tiff'; $ct['rtf'] = 'text/rtf'; $ct['wml'] = 'text/vnd.wap.wml'; $ct['wmls'] = 'text/vnd.wap.wmlscript'; $ct['xsl'] = 'text/xml'; $ct['xml'] = 'text/xml'; $extension = get_file_extension($value); if (!$type = $ct[strtolower($extension)]) { $type = 'text/html'; } return $type; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP to get File Type
On Tue, 2008-10-14 at 08:29 -0700, Yeti wrote: function get_file_extension($file) { http://us2.php.net/manual/en/function.pathinfo.php ?php $path_parts = pathinfo('/www/htdocs/index.html'); echo $path_parts['dirname'], \n; echo $path_parts['basename'], \n; echo $path_parts['extension'], \n; echo $path_parts['filename'], \n; // since PHP 5.2.0 ? Secondly the MIME type can differ from the extension (file suffix) It's the same with uploaded files. Although the browser sends the MIME type it might not be the right one, since browsers obtain the MIME by checking the extension. //A yeti Different browsers return different mime-types for the same file, so you cannot rely on that. The extension basically means squat with regards to the mime-type, as that can be changed, so don't rely on that. Basically, depending on what you want to achieve, there are several things you can do. Obviously http://br2.php.net/manual/en/function.mime-content-type.php and http://br2.php.net/manual/en/function.finfo-file.php have already been mentioned, but unfortunately these are not always available for every hosting package (I'm one of the unlucky ones!) The accepted method of detecting an image mime-type if you don't have access to the above functions is to use the GD library to perform a width and height calculation on the image. For other types, you can usually determine the type based on the first few bytes in the file. This is is simple representation of how a good operating system detects the mime-type (Windows relies on file extensions still.) For example, if all the characters are ascii, you could then use a bit of logic to determine if it's an XML-based file, Windows exe files begin with MZ and the character code 90, images usually start with a 3-letter representation of their type, etc. You might need to do a little digging with a hex-editor to find the file types you want, but it's generally useful to work on a whitelist basis, i.e. only accept file types you recognise and reject everything else. Hope this helps! Ash www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP to get File Type
function get_file_extension($file) { http://us2.php.net/manual/en/function.pathinfo.php ?php $path_parts = pathinfo('/www/htdocs/index.html'); echo $path_parts['dirname'], \n; echo $path_parts['basename'], \n; echo $path_parts['extension'], \n; echo $path_parts['filename'], \n; // since PHP 5.2.0 ? Secondly the MIME type can differ from the extension (file suffix) It's the same with uploaded files. Although the browser sends the MIME type it might not be the right one, since browsers obtain the MIME by checking the extension. //A yeti -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP to get File Type
Is there any function in PHP to get the file/Mime type of any file? check this out: http://us2.php.net/manual/en/function.finfo-open.php //A yeti -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP to get File Type
Manoj Singh wrote: Hello All, Is there any function in PHP to get the file/Mime type of any file? Any help will be appreciated. Hi, There are better and more elegant ways to do this, but I'm not aware of them. Here is what I use most of the time. You only use the filename as $value for get_mimetype(). function get_file_extension($file) { return array_pop(explode('.',$file)); } function get_mimetype($value='') { $ct['htm'] = 'text/html'; $ct['html'] = 'text/html'; $ct['txt'] = 'text/plain'; $ct['asc'] = 'text/plain'; $ct['bmp'] = 'image/bmp'; $ct['gif'] = 'image/gif'; $ct['jpeg'] = 'image/jpeg'; $ct['jpg'] = 'image/jpeg'; $ct['jpe'] = 'image/jpeg'; $ct['png'] = 'image/png'; $ct['ico'] = 'image/vnd.microsoft.icon'; $ct['mpeg'] = 'video/mpeg'; $ct['mpg'] = 'video/mpeg'; $ct['mpe'] = 'video/mpeg'; $ct['qt'] = 'video/quicktime'; $ct['mov'] = 'video/quicktime'; $ct['avi'] = 'video/x-msvideo'; $ct['wmv'] = 'video/x-ms-wmv'; $ct['mp2'] = 'audio/mpeg'; $ct['mp3'] = 'audio/mpeg'; $ct['rm'] = 'audio/x-pn-realaudio'; $ct['ram'] = 'audio/x-pn-realaudio'; $ct['rpm'] = 'audio/x-pn-realaudio-plugin'; $ct['ra'] = 'audio/x-realaudio'; $ct['wav'] = 'audio/x-wav'; $ct['css'] = 'text/css'; $ct['zip'] = 'application/zip'; $ct['pdf'] = 'application/pdf'; $ct['doc'] = 'application/msword'; $ct['bin'] = 'application/octet-stream'; $ct['exe'] = 'application/octet-stream'; $ct['class']= 'application/octet-stream'; $ct['dll'] = 'application/octet-stream'; $ct['xls'] = 'application/vnd.ms-excel'; $ct['ppt'] = 'application/vnd.ms-powerpoint'; $ct['wbxml']= 'application/vnd.wap.wbxml'; $ct['wmlc'] = 'application/vnd.wap.wmlc'; $ct['wmlsc']= 'application/vnd.wap.wmlscriptc'; $ct['dvi'] = 'application/x-dvi'; $ct['spl'] = 'application/x-futuresplash'; $ct['gtar'] = 'application/x-gtar'; $ct['gzip'] = 'application/x-gzip'; $ct['js'] = 'application/x-javascript'; $ct['swf'] = 'application/x-shockwave-flash'; $ct['tar'] = 'application/x-tar'; $ct['xhtml']= 'application/xhtml+xml'; $ct['au'] = 'audio/basic'; $ct['snd'] = 'audio/basic'; $ct['midi'] = 'audio/midi'; $ct['mid'] = 'audio/midi'; $ct['m3u'] = 'audio/x-mpegurl'; $ct['tiff'] = 'image/tiff'; $ct['tif'] = 'image/tiff'; $ct['rtf'] = 'text/rtf'; $ct['wml'] = 'text/vnd.wap.wml'; $ct['wmls'] = 'text/vnd.wap.wmlscript'; $ct['xsl'] = 'text/xml'; $ct['xml'] = 'text/xml'; $extension = get_file_extension($value); if (!$type = $ct[strtolower($extension)]) { $type = 'text/html'; } return $type; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] PHP to get File Type
Hello All, Is there any function in PHP to get the file/Mime type of any file? Any help will be appreciated. Regards, Manoj Kumar Singh