Home  >  Article  >  Backend Development  >  How to get the suffix of uploaded files? php strrev() function usage

How to get the suffix of uploaded files? php strrev() function usage

怪我咯
怪我咯Original
2017-04-28 17:43:433160browse

Almost every website has a file upload function. The file upload function is almost necessary for all websites. However, this function has great risks for the server. Therefore, we limit the size and type of uploaded files. For restrictions, you can limit the type of uploaded files by obtaining the suffix of the

uploaded file.

Get the suffix of the uploaded file

Get the suffix of the uploaded file You can use the strrev() function to reverse the name of the uploaded file and output it, and use the explode() function Use "." as the delimiter to split the file name, and then apply the strrev() function again to return the value of the first element in the array. What you get is the suffix of the uploaded file.

strrev() function is to output the string in PHP in reverse order. The syntax is as follows:

strrev(string)

The parameter string specifies the string that needs to be reversed

How to get the suffix of uploaded files? php strrev() function usageexplode() function breaks up the string into an array. For details, please see our String topic

Example

Previous paragraph The code to upload the file is as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <meta name="keywords" content=" keywords" />
    <meta name="description" content="description" />
</head>
<body>
<form method="post" action="" enctype="multipart/form-data">
    <input type="file" name="upfile" size="20" />
    <input type="submit" name="submit" value="上传" />
</form>
</body>
</html>

The code running result is as follows:

How to get the suffix of uploaded files? php strrev() function usage

Backend code

Create a PHP script file. When you click "Upload " button, first, use POST to receive the information in the text box. Secondly, use the inversion function to invert the text box information data, and use the string splitting function to split it with ".". Then, save the segmentation results in an array. Finally, reverse the specified data again to obtain and output the suffix name of the uploaded file. The code is as follows:

<?PHP
    if(isset($_POST[&#39;submit&#39;])) {
        $string = strrev($_FILES[&#39;upfile&#39;][&#39;name&#39;]);
        $array = explode(&#39;.&#39;,$string);
        echo $array[0];
    }    
?>

How to get the suffix of uploaded files? php strrev() function usage It is relatively troublesome to use the strrev() function to retrieve strings. You can use the regular expression function preg_match() to complete this function.

【Recommended related articles】

php method to use preg_match() function to verify ip address

The above is the detailed content of How to get the suffix of uploaded files? php strrev() function usage. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn