This extension is now a standard one. Why? Because it is a required extension when the Laravel Framework is installed, even the Laravel Framework cannot be used without opening it.

Introduce the Fileinfo

FileInfo is given a Magic byte sequence library to obtain the file’s content type and encoding. The sequence library it gets depends on the operating system. For example, the default file used in Linux is /usr/share/misc/magic. You can use this extension to get the MIME information of a file, such as image/ PNG, text/ HTML, etc.

The FileInfo extension is also designed to be compatible with both old and new development patterns, so it provides both procedural and object-oriented forms. Let’s take a look at how the object-oriented form is used.

Object-oriented use

$finfo = new finfo(FILEINFO_MIME); Echo $finfo->file("./ 1.php "), PHP_EOL; echo $finfo->file(". // text/x-php; charset=us-ascii echo $finfo->buffer(file_get_contents("https://www.baidu.com")) . "\n"; // text/html; charset=utf-8 $finfo->set_flags(FILEINFO_EXTENSION); echo $finfo->file('timg.jpeg') . "\n"; // jpeg/jpg/jpe/jfif

First, we get the file manipulation object by new a Finfo class. The constant in the parameter is optional. By default, it is FILEINFO_NONE, which means no special processing. Here we use FILEINFO_MIME to indicate that the file MIME type and encoding are returned in the format defined by RFC2045.

Then, using the file() method, you can get the MIME information for the specified file. The buffer() method returns information about the content of a string. For example, if we get the content of a web page, we can get that its string represents the file encoding format of text/ HMTL. The set_flags() method allows you to change the constructor parameter property of the object after it is instantiated, which is the parameter information we set when we instantiate it. In this case, we change it to FILEINFO_EXTENSION, which tells the FINFO object to return the file’s possible extension. We test with an image that returns possible extensions such as those shown in the comments.

Process oriented

For the object-oriented code above, we also show how the same operation can be performed using procedure-oriented functions.

$finfo = finfo_open(FILEINFO_MIME); Echo $finfo_file($finfo,"./ 1.php "), PHP_EOL; // text/x-php; charset=us-ascii echo finfo_buffer($finfo, file_get_contents("https://www.baidu.com")), PHP_EOL; // text/html; charset=utf-8 finfo_set_flags($finfo, FILEINFO_EXTENSION); echo finfo_file($finfo, 'timg.jpeg') . "\n"; // jpeg/jpg/jpe/jfif finfo_close($finfo);

You can see that the finfo object is replaced by the finfo_open() method to get a handle to the finfo operation. We then use the similar finfo_file(), finfo_buffer(), and finfo_set_flags() functions to achieve the same object-oriented results as above.

Note that procedure-oriented writing has a finfo_close() method, and there is usually a close function for handling type operations that frees the handle resource. As with extensions such as mysqli, finfo contains one such function and provides only the procedure-oriented function; the finfo class above does not have such a close() method.

Quick return to MIME

Of course, the FileInfo extension also gives us a function to quickly return the MIME information of the file. The MIME information of a file can be obtained quickly and easily without using a FINFO object or opening a FINFO handle.

Echo mime_content_type('./ 1.php '), PHP_EOL; // text/x-php echo mime_content_type('./timg.jpeg'), PHP_EOL; // image/jpeg

However, PHP officials seem to have deprecated this function, but have now restored it, which means it is not particularly recommended. In the formal development process, we don’t bother to use FINFO objects or FINFO related functions to obtain MIME information.

If we are sure that the only file we want to determine is the image type, we can also use another function to perform the MIME fetch of the image file.

$image = exif_imagetype("./timg.jpeg"); 
echo image_type_to_mime_type($image), PHP_EOL;
// image/jpeg

conclusion

Very simple but very useful function, why is it useful? Upload file security issues can rely on it to solve. When uploading a file, we usually determine the suffix name of the file and the MIME type of the file in the upload array. However, many tools can change the MIME Type of the file during the upload process, that is, through some packet capture tools to change the content-type. The FileInfo extension is used to get files that must already exist locally or remotely, which means that there are no security check bypasses for modifying the transfer information during the upload process.

So, in the LaVarl framework, Vendor/laravel/framework/SRC/Illuminate/Filesystem/Filesystem. The mimeType on the PHP class () method USES is finfo_file () this function to get the mime the file Information. In its upload component, Laravel’s underlying symfony framework also uses the finfo_file() function for the MIME determination of uploaded files. (vendor/symfony/mime/FileinfoMimeTypeGuesser. PHP) and not directly using the normal type field in the $_FILES after upload.

Test code:

https://github.com/zhangyue0503/dev-blog/blob/master/php/202010/source/5. Learn to use.php for the FileInfo extension in PHP

Reference documents:

https://www.php.net/manual/zh/book.fileinfo.php

= = = = = = = = = = =

Search for “Hardcore Project Manager” on all media platforms