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