Php Upload function set suffix judgment and random naming (strongly recommended), php strongly recommended _ PHP Tutorial

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

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.