"PHP" process-oriented file upload process

Source: Internet
Author: User

1 //perform a full file upload2 $path= "./uploads";//specified directory for file upload3 $upfile=$_files[' pic '];//File upload Information "one-dimensional array"4 $typeList=Array(' Image/jpeg ', ' image/png ', ' image/gif ');//List of file types to allow users to upload5 $maxSize= 0;//sets the range of upload file size, if 0 means no Limit size6 7 //format path information8 $path=RTrim($path,"/")." /";9 Ten //1. Judging the error number One if($upfile[' Error ']>0){ A     Switch($upfile[' Error ']){ -          Case1: -             $info= "indicates that the size of the uploaded file exceeds the agreed value"; the          Break; -          Case2: -             $info= "indicates that the upload file size exceeds the maximum value specified by the Max_file_size element of the HTML form's hidden field property"; -          Break; +          Case3: -             $info= "indicates that the file is only partially uploaded"; +          Break; A          Case4: at             $info= "indicates that no files were uploaded"; -          Break; -          Case6: -             $info= "indicates no temporary folder found"; -          Break; -          Case7: in             $info= "indicates file write Failed"; -          Break; to     } +      die("Upload failed!") Reason: {$info}"); - } the  * //2. Determine the type of upload file $ if($typeList&&Count($typeList) >0){Panax Notoginseng     if(!In_array($upfile[' type '],$typeList)){ -          die(' Upload failed! Reason: The system does not allow the upload file type! ‘); the     } +}Else{ A      die(' Upload failed! Cause: The system does not define a list of files that are allowed to be uploaded! ‘); the } +  - //3. Determine the size of the upload file $ if($maxSize>0 &&$upfile[' Size ']>$maxSize){ $      die(' Upload failed! Cause: The upload file size is beyond the allowed range of the system! ‘); - } -  the //4. Create a new file name to avoid duplicate names - $ext=PathInfo($upfile[' name '],pathinfo_extension);//Get suffix nameWuyi  Do{ the     $newName=Date(' Ymdhis ').Rand(100000000,999999999). ".".$ext; -} while(file_exists($path.$newName)); Wu  - //5. Perform file movement About if(Is_uploaded_file($upfile[' Tmp_name '])){ $     if(Move_uploaded_file($upfile[' Tmp_name '],$path.$newName)){ -         Echo"Congratulations, upload success!" File name: ".$path.$newName; -}Else{ -          die(' Upload failed! Reason: Mobile upload file failed! ‘); A     } +}Else{ the      die(' Upload failed! Reason: Not a valid upload file! ‘); -}

"PHP" process-oriented file upload process

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.