Almost every website has the file upload function. the file upload function is essential to almost all websites. However, this function is highly risky for servers, we restrict the size and type of the uploaded file. we can obtain the suffix of the uploaded file to restrict the type of the uploaded file. Almost every website has the file upload function. the file upload function is essential to almost all websites. However, this function is highly risky for servers, we can restrict the size and type of the uploaded files by obtaining
The suffix of the uploaded file to restrict the type of the uploaded file.
Get the suffix of the uploaded file
To obtain the suffix of an uploaded file, you can use the strrev () function to reverse the output of the name of the uploaded file, and use the explode () function ". "As the separator, the file name is separated, and then the strrev () function is applied again to return the first element value in the array. the obtained suffix is the uploaded file.
The strrev () function outputs the strings in PHP in reverse order. The syntax is as follows:
strrev(string)
The string parameter specifies the string to be reversed.
The explode () function splits strings into arrays. for details, refer to our string topic.
Example
The code for uploading files in the previous section is as follows:
The code execution result is as follows:
Backend code
Create a PHP script file. when you click "upload", first use POST to receive information in the text box. Second, the inverted function is used to reverse the text box information data, and the string split function is used to separate the text box information. Then, save the split result in the array. Finally, the suffix name of the uploaded file is obtained and output after the specified data is reversed. the code is as follows:
Using the strrev () function to retrieve strings is relatively troublesome. you can use the regular expression function preg_match () to complete this function.
[Recommended related articles]
Php uses the preg_match () function to verify the IP address
The above figure shows how to obtain the suffix of the uploaded file? Php strrev () function usage details. For more information, see other related articles in the first PHP community!