PHP multi-File Upload code exercise

Source: Internet
Author: User

HTML form:

<HTML>    <Head><title>Upload file</title>    <Metahttp-equiv= "Content-type"content= "Text/html;charset=utf-8"/>    </Head>    <Body>        <formAction= "test.php"Method= "POST"enctype= "Multipart/form-data">            <!--name must be Max_file_size,value is the number of bytes -            <inputtype= "hidden"name= "Max_file_size"value= "2097152"/>            <!--Accept is the MIME type of the file -            <!--nultiple= "Nultiple" is the representative can choose multiple -            <inputtype= "File"name= "filename[]"multiple= "multiple"Accept= "image/jpeg,image/gif,image/png,application/x-ms-bmp,text/plain,text/html" /><BR><BR>            <inputtype= "Submit"value= "Upload file"/>        </form>    </Body></HTML>

PHP multi-File Upload function form uploads.func.php

<?PHPfunctionGetFiles () {//change the three-dimensional array to a two-dimensional array;    $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++; #code ...            }        }ElseIf(is_string($file[' Name '])) {            $files[$i]=$file; $i++; #code ...        }         #code ...     }     return $files;}functionUploadfiles ($files,$path= ' uploads ',$maxsize=2097152){     $result=Array(); //Judge $files[' error ']    if($files[' Error ']===UPLOAD_ERR_OK) {        //determine file size//$maxsize =2097152;        if($files[' Size ']>$maxsize) {            $result[' Msg ']=$files[' name ']. ' File too large '; #code ...        }        //determine file format        $ext[Email protected]Strtolower(End(Explode(‘.‘,$files[' Name ']))); $extarray=Array(' gif ', ' jpeg ', ' jpg '); if(!In_array($ext,$extarray)) {            $result[' Msg ']=$files[' name ']. ' File format is incorrect '; #code ...        }        //Check if it's a real picture        $check=true; if($check) {                        if(@!getimagesize($files[' Tmp_name '])){                $result[' Msg ']=$files[' name ']. ' Not a real picture '; }            #code ...        }        //determine if the post is uploaded via HTTP        if(!Is_uploaded_file($files[' Tmp_name '])) {            $result[' Msg ']=$files[' name ']. ' Not via HTTP POST upload '; #code ...        }        if($result)return $result;//return to $result; Move File//Specify upload directory to determine and create directory//$path = ' uploads ';        if(!file_exists($path)) {            mkdir($path, 0777,true); chmod($path, 0777); #code ...        }        //a unique file name        $newname=MD5(uniqid(Microtime(true),true)).‘.‘.$ext; $destination=$path.‘ /‘.$newname; if(!Move_uploaded_file($files[' Tmp_name '],$destination)){            $result[' Msg ']=$files[' name ']. ' File move failed '; }        $result[' Msg ']=$files[' name ']. ' Upload succeeded '; $result[' Dest ']=$destination; return $result; }Else{        Switch($files[' Error ']){             Case1:$result[' Msg ']=$files[' Name ']. "The file exceeds the value of the Upload_max_filesize option limit in php.ini. ";  Break;  Case2:$result[' Msg ']=$files[' name ']. " The size of the file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form. "; #code ...                    Break;  Case3:$result[' Msg ']=$files[' name ']. " Files are only partially uploaded "; #code ...                    Break;  Case4:$result[' msg ']= ' No files were uploaded '; #code ...                    Break;  Case6:$result[' msg ']= ' cannot find the Temp folder. "; #code ...                    Break;  Case7: Case8:$result[' msg ']= ' system error '; #code ...                    Break; }        return $result; }    }?>

upload.php

<?PHPinclude(' uploads.func.php ');$files=getFiles ();foreach($files  as $value) {    $result=uploadfiles ($value); Echo $result[' msg ']. " <br/> "; $uploads[][email protected]$result[' Dest '];} $uploads=array_values(Array_filter($uploads)); Print_r($uploads);//when the callback of the Array_filter function is left blank, he filters out all keys with a key value of false//array_values () function returns an array containing all the key values in the given array, but does not retain the key name. 

PHP multi-File Upload code exercise

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.