How does PHP identify file camouflage?
This article mainly explains "how to identify document camouflage by PHP". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "PHP how to identify file camouflage"!
Question:
When uploading a file, you need to verify whether the uploaded file is legal. How to identify the file camouflage?
A simple test: directly change the txt file suffix to jpg; upload
Test Send this file:
1. Get the file suffix name through $_ FILES ['userfile'] [' type'];
$data = $_ FILES ['userfile']; var_dump ($data); / * * result * * / / * array (5) {["name"] = > string (8) "test.jpg" ["type"] = > string (10) "image/jpeg" ["tmp_name"] = > string (26) "/ private/var/tmp/phpfyE3EC" ["error"] = > int (0) ["size"] = > int (19)} * /
It was not detected.
two。 Use the pathinfo () function to get the information of the file path
$data = $_ FILES ['userfile']; / / var_dump ($data); var_dump (pathinfo ($data [' name'])); / * result * / / * array (4) {["dirname"] = > string (1) "." ["basename"] = > string (8) "test.jpg" ["extension"] = > string (3) "jpg" ["filename"] = > string (4) "test"} * /
It was not detected.
3.PHP 's extended fileinfo (needs to be installed and enabled)
$data = $_ FILES ['userfile']; $filename = $data [' tmp_name']; $finfo = finfo_open (FILEINFO_MIME_TYPE); / / returns the mime type. Available since PHP 5.3.0. $mimetype = finfo_file ($finfo, $filename); finfo_close ($finfo); var_dump ($mimetype); / * * result * / / string (10) "text/plain" so far, I believe you have a better understanding of "how PHP identifies file camouflage". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!