How can I obtain the suffix of an uploaded file? Phpstrrev () function usage

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.