A few days ago wrote to use the WordPress XMLRPC API Remote Publishing articles, if the local server article library in a post to be published in a WordPress article contains image files, will use the WordPress upload file API Metaweblog.newmediaobject, the API needs to provide the MIME type of the file. How does PHP get the MIME type of a file (picture)? The original remote blog uses the PHP mime_content_type () function, which is fully supported after testing with the default configuration of Ubuntu server lamp for development, returning the correct file MIME type. However, when porting the API project to the CentOS 5.2 (kernel 2.6) lamp environment, the following error message appears:
Fatal error:call to undefined Function:mime_content_type ()
Finally, the latest PHP manual found that PHP mime_content_type () function has been deprecated, of course, the official is not recommended, and need to pass the appropriate PHP configuration to use. So to get the mime type of a picture or other file, the Fatal error:call to undefined Function:mime_content_type () error has the following solutions.
Mime_content_type () function to determine which MIME type to get
If you still love the Mime_content_type () function that has been discarded by PHP 5.3.0, you can configure PHP to enable Magic_mime extensions. For example, under CentOS use Phpinfo () view PHP Apache configuration, find to Mime-magic, if the "--without-mime-magic", then to compile PHP switch to "with-mime-magic" option. The Mime_content_type () function also relies on the Apache httpd Magic File (Mime_magic.magicfile), in order to detect the MIME type of the file, you must configure the address that tells the magic file, such as ' –with- Mime-magic=/usr/share/file/magic.mime '. The Windows environment also needs to be added in php.ini:
Mime_magic.magicfile = "$PHP _install_dirmagic.mime"
Where $php_install_dir is your PHP installation directory. In some lamp environments, this mime_magic file may not exist or be readable, and it must be downloaded separately. In addition, some virtual hosts do not necessarily return the correct MIME type for security reasons, sometimes returning an empty string, even if there are with-mime-magic. Therefore, it is not recommended to use this method to get the file MIME type, since the Mime_content_type () function has been deprecated.
PHP Fileinfo Get file MIME type (finfo_open)
PHP official recommendation The alternative function of Mime_content_type () is the FileInfo function. PHP 5.3.0+ already supports the FileInfo function (FileInfo support-enabled) By default, and you can use Finfo_open () to determine the file MIME type without having to make any configuration. Centos default installation of the lamp environment PHP version or PHP5.2.6, less than 5.3.0 version may appear similar error prompt:php Fatal error:call to undefined function finfo_open () in< /c2> .... Because of the previous PHP version, the Magic_open class needs to be loaded, the FileInfo function belongs to the pecl extension, and the FileInfo pecl extension is enabled to detect MIME types. So there are two ways to get the MIME type of a file using FileInfo.
Span style= "FONT-SIZE:14PX;" >image_type_to_mime_type () Gets the picture MIME type
If we need to determine that MIME-type files are only image files, you can first use the exif_ The ImageType () function gets the image type constants, and then uses the Image_type_to_mime_type () function to convert the image type constants to the MIME type of the picture file. The same php.ini to be configured to open Php_mbstring.dll (Windows requires) and Extension=php_exif.dll. Phpinfo () "–enable-exif ". First Exif_imagetype returns the image type constants (Imagetype Constants), such as Imagetype_gif, Imagetype_jpeg, Imagetype_png, and so on.
<?= Exif_imagetype ( "D: Farleeinfo.jpg "); //local path or remote picture address can imagetype_gif// $image Span class= "pun" >= Exif_imagetype (); $mime = Image_type_to_mime_type ( Span class= "PLN" > $image ); echo $mime ; //output image/jpeg< Span class= "PLN" > ?>
PHP upload file get MIME type
If you upload a file using PHP to detect the MIME type of the uploaded file, you can use the global variable $_files[' uploadfile ' [' type '], which is detected by the client's browser to get the file MIME type.
In the Centos system or other environment if it is not convenient to get file MIME type, there is the last absolute feasible method, is to read the file name suffix, according to the suffix name one by one corresponding file MIME type, specifically can refer to the PHP Manual of this comment. Of course, the MIME file type detected by this method is not necessarily very accurate.
PHP get file MIME type FileInfo etc methods