1<?PHP2 /**3 * File Upload4 * @param array $file file upload information (is an array with 5 elements)5 * @param array $allow The type of file upload allowed6 * @param string & $error reference passed to log error messages7 * Upload directory @param string $path file8 * @param int $maxsize = 1024*1024 Allow file Upload size9 * @param mixed false/$newname If the upload fails, return false, and the new name of the file will be returned successfully.Ten */ One A functionUpload$file,$allow,&$error,$path,$maxsize=1048576){ - Switch($file[' Error ']){ - Case1:$error= ' The uploaded file exceeds the value of the option limit! '; the return false; - Case2:$error= ' the size of the uploaded file exceeds the value specified by the option! '; - return false; - Case5 |$error= ' files are only partially uploaded! '; + return false; - Case4:$error= ' No file selected ' was uploaded! '; + return false; A Case6: at Case: W$error= ' File write failed, System busy! '; - return false; - } - - //determine the size of a file - if($file[' Size '] >$maxsize){ in $error= ' File upload too big! '; - return false; to } + - if(!In_array($file[' type '],$allow)){ the $error= ' File upload type does not exist, allow upload is type: '.implode(‘,‘,$allow); * return false; $ }Panax Notoginseng - $newname= Randname ($file[' Name ']); the $target=$path. ‘/‘ .$newname; + $result=Move_uploaded_file($file[' Tmp_name '],$target); A if($result){ the return $newname; +}Else{ - $error= ' An unknown error occurred and the upload failed! ‘; $ return false; $ } - } - the /** - * Generate a random name function, file name = Current time plus random numberWuyi * @param string $filename file original name the * @return A new name for string $newname file - */ Wu functionRandname ($filename){ - $newname=Date(' Ymdhis '); About //random numbers that follow $ $str= "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - for($i= 0;$i< 6;$i++){ - $newname.=$str[Mt_rand(0,strlen($str)-1)]; - } A $newname.=STRRCHR($filename,‘.‘); + return $newname; the}
Upload Image function