Form:
<HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /> <title>No title</title></Head><Body> <formAction= "upload.php"Method= "POST"enctype= "Multipart/form-data"> <inputtype= "hidden"name= "Max_file_size"value= "2097152"> <inputtype= "File"name= "filename[]"multiple= "multiple"Accept= "Image/jpeg"><BR/><BR/> <inputtype= "Submit"value= "Upload"> </form></Body></HTML>
Function section:
<?PHP//determine if it is a single file or multiple files and combines arraysfunctionGetFiles () {$i=0; foreach($_files as $file) { if(Is_array($file[' Name '])) { foreach($file[' Name '] as $key=$value) { $files[$i[' Name ']=$file[' Name '] [$key]; $files[$i[' Type ']=$file[' type '] [$key]; $files[$i[' Tmp_name ']=$file[' Tmp_name '] [$key]; $files[$i[' Size ']=$file[' Size '] [$key]; $files[$i[' Error ']=$file[' ERROR '] [$key]; $i++; } }ElseIf(is_string($file[' Name '])) { $files[$i]=$file; $i++; #code ... } } return $files;}//Upload file functionfunctionUploadfiles ($files){ $res=Array(); //Judging the error number if($files[' Error ']===UPLOAD_ERR_OK) { //Determine file size $maxsize=2097152; if($files[' Size ']>$maxsize){ $res[' Msg ']=$files[' name ']. ' File too large '; } //determine if the file format meets the requirements $ext=Strtolower(PathInfo($files[' Name '],pathinfo_extension)); $extarray=[' jpeg ', ' jpg ']; if(!In_array($ext,$extarray)){ $res[' Msg ']=$files[' name ']. ' Format does not meet the requirements '; #code ... } //Judging whether it's a real picture $flag=true; if($flag){ if(@!getimagesize($files[' Tmp_name '])){ $res[' Msg ']=$files[' name ']. ' Not a real picture file '; } } //determine if it is uploaded via HTTP post; if(!Is_uploaded_file($files[' Tmp_name '])){ $res[' Msg ']=$files[' name ']. ' Not uploaded via HttpPost '; } if($res)return $res; //Moving Files $path= ' uploads '; if(!file_exists($path)){ mkdir($path, 0777,true); chmod($path, 0777); } //a unique file name $newname=MD5(uniqid(Microtime(true),true)); $destination=$path.‘ /‘.$newname.‘.‘.$ext; if(!Move_uploaded_file($files[' Tmp_name '],$destination)){ $res[' Msg ']=$files[' name ']. ' File move failed '; } $res[' Msg ']=$files[' name ']. ' Upload succeeded '; $res[' Dest ']=$destination; return $res; }Else{ Switch($files[' Error ']) { Case1:$res[' Msg ']=$files[' name ']. ' The file exceeds the maximum limit '; Break; Case2:$res[' Msg ']=$files[' name ']. ' The size of the file exceeds the form limit '; #code ... Break; Case3:$res[' Msg ']=$files[' name ']. ' Only partially uploaded '; #code ... Break; Case4:$res[' msg ']= ' no file selected '; #code ... Break; Case6:$res[' msg ']= ' cannot find temp file '; #code ... Break; Case7:$res[' msg ']= ' system error '; #code ... Break; } return $res; }}
Operation section:
<? PHP include (' uploads_func.php '); $files=getFiles (); foreach ($files as $value ) { $res=uploadfiles ($value); Echo $res [' msg ']. " <br><br> "; # code ...}
PHP upload file Code Exercise 2 (Repeat article)