Almost every website has the function of file upload, the file upload function is almost necessary for all sites, however, this feature is a great risk to the server, so we have to upload the size and type of file to limit, you can get
Upload a file suffix to limit the type of file you upload.
Get the suffix of the uploaded file
The suffix of the upload file can be reversed by the Strrev () function to the name of the uploaded file, and the explode () function is used to "." As a delimiter, the file name is split, and then the Strrev () function is applied again to return the output of the first element value in the array, and the suffix of the uploaded file is obtained.
The Strrev () function is an inverse output of a string in PHP, with the following syntax:
Strrev (String)
The argument string is the string that specifies that the reversal is required
The explode () function breaks the string into arrays, specifically to see our string feature
Example
The preceding section uploads the file code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
The result of the code operation is as follows:
Back-end Code
Create a PHP script file and when you click on the "Upload" button, first use post to receive the information in the text box. Second, use the inverse function to invert the text box information data and use the string splitter function to "." To split. Then, the split result is saved in the array. Finally, the specified data is reversed again to get and output the suffix name of the uploaded file, the code is as follows:
<? PHP if (isset ($_post[' submit ')) { $string = Strrev ($_files[' upfile ' [' name ']); $array = Explode ('. ', $string); echo $array [0]; } ? >
Using the Strrev () function to retrieve a string is relatively cumbersome, and you can use the regular expression function Preg_match () to complete the function.