According to my personal experience, when processing File Uploads in php, the following methods can be often used to determine the file type through the filename suffix, which is not secure and can be easily determined by mime, some types of files can be identified by the header byte by modifying the file suffix... based on my personal experience, when processing File Uploads in php, you can often use the following methods to determine the file type:
- It is difficult to cheat by filename suffix
- It is determined by mime that some types of files can be spoofed to the server by modifying the file suffix.
- The file type is determined by the header bytes, but the judgment range is limited. For example, a new document such as docx/xlsx is actually a zip package when the header information is used for judgment.
Is there any other way to obtain the file type more reasonably and securely?
Reply content:
Based on my personal experience, when processing File Uploads in php, you can often use the following methods to determine the file type:
- It is difficult to cheat by filename suffix
- It is determined by mime that some types of files can be spoofed to the server by modifying the file suffix.
- The file type is determined by the header bytes, but the judgment range is limited. For example, a new document such as docx/xlsx is actually a zip package when the header information is used for judgment.
Is there any other way to obtain the file type more reasonably and securely?
In my opinion, the subject needs to ensure the security of file upload.
First, you must set the file type and size hard. In addition, I personally thinkmimeThe type determination is safe. If the subject is still insecure, you can read the file header to determine the actual type.
The method can be referred to in this case:
PHP determines the actual type of a file and its application example by reading two bytes in the file header
When I see the content in the question of the subject, I think the three methods mentioned by the subject will increase the security level in sequence. If these three methods are implemented layer by layer, I think the security should be guaranteed.
Determine the header first. If the image is uploaded to gd, use openoffice if the document is used.
Fake ones will also turn you into real ones.