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>