Php Upload function set suffix judgment and random naming (strongly recommended), php strongly recommended. Php Upload function set suffix judgment and random naming (strong recommendation). php strong recommendation is not nonsense. for details, see the following code example. Form. phphtmlheadmetahttp-equivcontent-t php Upload function set suffix judgment and random naming (strongly recommended), php strongly recommended
No nonsense. For more information, see the following code example.
Form. php
Upload Image
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" Back ";}}
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" Back ";}}?>
Upload Image
The above content is the php Upload function set suffix judgment and random naming (strongly recommended.
Strong (strong recommendation), php strong recommendation is not nonsense, please refer to the following code example for details. Form. php htmlhead meta http-equiv = "content-t...