$fileInfo = @$_files [' myFile '];//receive form uploaded files//5 parameters, the first one is must, followed by default values upload ($fileInfo, "626800", False, "IMGs", Array ("TXT")); Function upload ($fileInfo, $maxSize =2097152, $flag =true, $path = "Upload", $ Allowedext=array ("JPEG", "JPG", "png", "GIF")) {//$maxSize = 2 * 1024 * 1024; // upload file size limit within 2M//$flag = true;// Judging error number if ($fileInfo [' ERROR '] > 0) {switch ($fileInfo [' error ') {case 1 : $mes = ' The upload file exceeds the value of the upload_max_filesize option in the PHP configuration file '; break;case 2 : $mes = ' upload file exceeds the size of the form max_file_size limit ' ; break;case 3 : $mes = ' files are uploaded '; break;case 4 : $mes = ' no files selected for upload '; BREAK;CASE 6&NBSP: $mes = ' no temporary directory found '; break;case 7 :case 8 : $mes = ' system error '; exit ( $mes );} Determine the file type// $ext =strtolower (End (Explode (".", $fileInfo [' name '])); //get the suffix name, or use the following method $EXT&Nbsp;= pathinfo ( $fileInfo [' name '], pathinfo_extension );// $allowedExt = array (// "jpeg",// "jpg",// "png",// "GIF" // )//if (!is_ Array ($ALLOWEDEXT)) {exit ("The file name extension is not an array");} if (! in_array ( $ext, $allowedExt )) {exit ( "Illegal file type" );} print_r (getimagesize ($fileInfo [' tmp_name '));exit;// determine if it is the true picture file type, if you do not need to do this detection can be set $flag for Falseif ($flag) {if (! getimagesize ( $fileInfo [' tmp_name '] )) {exit ( "Upload file is not a real picture file" );}} Determine file size if ($fileInfo [' size '] > $maxSize) {exit ( "Upload file size exceeds this site limit" );} Whether is uploading via post, it is necessary to detect the temporary file// var_dump (is_uploaded_file ($fileInfo [' tmp_name '));exit;if (! is_uploaded_file ( $fileInfo [' tmp_name '] ) {exit ( "Illegal upload method" );} Move files after detection//$path = "upload";if (! file_exists ( $path )) {mkdir ( $path, 0755, true ) chmod ( $path, 0755 );} $uniqName =md5 ( uniqid ( microtime ( true ), true ) ) . "." . $ext $destination = $path . "/" . $uniqName;if (! @move_ uploaded_file ( $fileInfo [' tmp_name '], $destination )) {// plus @ is to not display overflow error exit ( "File upload failed" );} echo "File Upload successful! ";}
This article is from a "sense of direction" blog, be sure to keep this source http://itech.blog.51cto.com/192113/1606734
Optimized single-File upload function