How do I get the suffix of an uploaded file? PHP strrev () function usage

Source: Internet
Author: User
Tags string splitter
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.

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.