PHP uses the finfo_file () function to detect the implementation of the uploaded image type,
This example describes how PHP uses the finfo_file () function to detect the uploaded image type. We will share this with you for your reference. The details are as follows:
In input and output, file interaction is essential, such as file upload. Here we will solve a small problem, that is, how to determine the file type uploaded by the user.
For exampleApplication Scenario: The segment is forcibly modified to nowamagic.png, so that the program cannot cut this type of image, and the cut fails. In this case, it is necessary to check the file, and it cannot be determined only by the suffix.
PHP certainly comes with some functions to implement such a common requirement. This function isfinfo_file
.
finfo_file
(PHP> = 5.3.0, PECL fileinfo> = 0.1.0)
Finfo_file -- finfo: file-Return information about a file, This function is used to get information about a file.
The function is very simple. You can see the official document clearly. Here is an example of how to upload an image and determine its type:
<Html> <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8 ">
Note:Yes,The finfo_file function requires PHP version> = 5.3.0. If it is lower than 5.3, you need to remove the semicolon before extension = php_fileinfo.dll in php. ini to enable it..
Of course, there are many ways to detect file types, which will be discussed later.