Php Upload function set suffix judgment and random naming, php Upload suffix naming

Source: Internet
Author: User

Php Upload function set suffix judgment and random naming, php Upload suffix naming

Form. php

Upload. php

<? Phpinclude ("check. php "); // introduce the UDF File $ type = array (" jpg "," gif "," bmp "," jpeg "," png "); // determine the type of the uploaded file $ fileext = strtolower (fileext ($ _ FILES ['file'] ['name']); $ uploadfilename = random (8 ); if (in_array ($ fileext, $ type) {$ filename = explode (". ", $ _ FILES ['file'] ['name']); if (is_uploaded_file ($ _ FILES ['file'] ['tmp _ name']) {// echo $ _ FILES ['file'] ['tmp _ name']; $ flag = move_uploaded_file ($ _ FILES ['file'] ['tmp _ na Me '], "/Library/WebServer/Documents/test/". $ uploadfilename. ".". $ fileext); if ($ flag) {echo "Upload successful! ";}Else {echo" Error. ";}echo" <a href = 'javascript: history. go (-1) '> Back </a> ";}}

Check. php

<? Phpheader ("Content-type: text/html; charset = utf8"); // obtain the file suffix function fileext ($ filename) {$ sTemp = strrchr ($ filename ,". "); return substr ($ sTemp, 1);} function fileext2 ($ filename) {$ sTemp = explode (". ", $ filename); return $ sTemp [count ($ sTemp)-1];} // generate the random file name function random ($ length) {$ captchaSource = "0123456789abcdefghijklmnopqrstuvwxyz this is an example of a random Print Output string"; $ captchaResult = "2015"; // return value of a random number $ captchaS Entry = ""; // random number intermediate variable for ($ I = 0; $ I <$ length; $ I ++) {$ n = rand (0, 35 ); # strlen ($ captchaSource); if ($ n >=36) {$ n = 36 + ceil ($ n-36)/3) * 3; $ captchaResult. = substr ($ captchaSource, $ n, 3);} else {$ captchaResult. = substr ($ captchaSource, $ n, 1) ;}} return $ captchaResult ;}?>

Integrate the three files into one:

<? Php // obtain the file extension function fileext ($ filename) {$ sTemp = strrchr ($ filename ,". "); return substr ($ sTemp, 1);} function fileext2 ($ filename) {$ sTemp = explode (". ", $ filename); return $ sTemp [count ($ sTemp)-1];} // generate the random file name function random ($ length) {$ captchaSource = "0123456789abcdefghijklmnopqrstuvwxyz this is an example of a random Print Output string"; $ captchaResult = "2015"; // return value of a random number $ captchaResult chas= ""; // random number intermediate variable for ($ I = 0; $ I <$ length; $ I + +) {$ N = rand (0, 35); # strlen ($ captchaSource); if ($ n> = 36) {$ n = 36 + ceil ($ n-36)/3) * 3; $ captchaResult. = substr ($ captchaSource, $ n, 3);} else {$ captchaResult. = substr ($ captchaSource, $ n, 1) ;}return $ captchaResult;} $ type = array ("jpg", "gif", "bmp", "jpeg ", "png"); // determine the type of the uploaded file $ fileext = strtolower (fileext ($ _ FILES ['file'] ['name']); $ uploadfilename = random (8); if (in_array ($ fileext, $ type )){ $ Filename = explode (". ", $ _ FILES ['file'] ['name']); if (is_uploaded_file ($ _ FILES ['file'] ['tmp _ name']) {// echo $ _ FILES ['file'] ['tmp _ name']; $ flag = move_uploaded_file ($ _ FILES ['file'] ['tmp _ name'], "/Library/WebServer/Documents/test /". $ uploadfilename. ". ". $ fileext); if ($ flag) {echo "uploaded successfully! ";} Else {echo" Error. ";} echo" <a href = 'javascript: history. go (-1) '> Back </a> ";}}?> <Html> 

 

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.