PHP File upload multiple file upload

Source: Internet
Author: User
Tags http post php file upload
Before the nonsense, the first statement, this article is based on the master PHP single file upload, so here do not repeat the file Upload server configuration, the form to set the attention of the place.

Not many words, straight into the topic, in the request page has two ways of writing (only the form part, to upload three files as an example. )

<action= "doaction.php"  method= "POST"  enctype= " Multipart/form-data ">         Please        select my upload file <type = "File" name = "myfile[]" /> <  type= "file"  name= "myfile[]"/><    type = "File" name = "myfile[]"/>   <type= "Submit"  value= "Upload"/> 
  
    form > 

<action= "doaction.php"  method= "POST"  enctype= " Multipart/form-data ">         Please        select my upload file <type = "File" name = "Myfil1" /> <  type= "file"  name= "Myfil2"/><  type= "file"  name= "myfil3"/><  type= "Submit"  value= "Upload"/> 
  
    form> 

In two comparisons, it is found that only the name is different, the first one sets the name to the array form, and the second is a method that we usually set and easily think of.

While the surface shows only a little bit different, the $_files that are actually submitted to the doaction.php page are quite different.

The first type of $_files is a three-dimensional array, and the second is a two-dimensional array, as follows:

Obviously we are more convenient in dealing with the $_files of the second format. Of course, we can also find a way to convert the $_files of the first format into a second form, as follows: \

functionGetFiles () {foreach($_files as$file){        $fileNum=Count($file[' Name ']); if($fileNum==1) {            $files=$file; }Else{                         for($i= 0;$i<$fileNum;$i++) {                 $files[$i[' Name ']=$file[' Name '] [$i]; $files[$i[' Type ']=$file[' type '] [$i]; $files[$i[' Tmp_name ']=$file[' Tmp_name '] [$i]; $files[$i[' Error ']=$file[' ERROR '] [$i]; $files[$i[' Size ']=$file[' Size '] [$i]; }        }            }    return$files;}

With this function, the $_files is converted to the following format:

At the moment, the two upload methods are already at the same starting line, the following work is to write UploadFile () function to upload each file, this is the focus of this article.

File Upload function:

functionUploadFile ($file,$path= './uploads ',$max _size,$allowExt) {$filename=$file[' Name ']; $type=$file[' Type ']; $temp _name=$file[' Tmp_name ']; $error=$file[' Error ']; $size=$file[' Size ']; if($error==UPLOAD_ERR_OK) {        if($size>$max _size) {            $res[' Mes ']=$filename." File exceeds the specified upload size "; }        $ext=getext ($filename); if(!In_array($ext,$allowExt)) {            $res[' Mes ']=$filename.' File name is not compliant '; }        if(!Is_uploaded_file($temp _name)) {            $res[' Mes ']=$filename." The file is not uploaded via the HTTP POST method.; }        if(@$res) {            return$res; }        if(!file_exists($path)) {            mkdir($path, 0777,true); chmod($path, 0777); }        $fname=Getuniname (); $destination=$path.' /'.$fname.'.'.$ext; if(Move_uploaded_file($temp _name,$destination)) {            $res[' Mes ']=$filename.' Upload succeeded '; $res[' Dest ']=$destination; }Else{            $res[' Mes ']=$filename." File upload Failed "; }    }Else{        Switch($error) {             Case' 1 ':$res[' Mes ']= ' exceeds the size of the profile upload file ';  Break;  Case' 2 ':$res[' Mes ']= ' exceeds the size of the form settings upload file file ';  Break;  Case' 3 ':$res[' Mes ']= ' file part was uploaded ';  Break;  Case' 4 ':$res[' Mes ']= ' no files were uploaded ';  Break;  Case' 6 ':$res[' Mes ']= ' did not find temp directory ';  Break;  Case' 7 ':$res[' Mes ']= ' file is not writable ';  Break; default:$res[' mes ']= ' upload file failed ';  Break; }    }    return$res;}

There are also two small functions involved:

/* * * Get file extension * @param  string $filename upload file name * @return String           */function getext ($ FileName) {    $arr=explodebasename($filename));     return End ($arr);} /*  */function  getuniname ()    {returnMD5(  Uniqid(microtime(true),true)); }

Feel:

A long time ago contacted php file upload, then feel mess. Now it seems that as long as the $_files contain what information, the use of their own knowledge to deal with some small skills, the system has a logical consideration, timely encapsulation function, the file upload can be quickly launched. This time I show the code must not be able to meet any needs, so can be properly modified to become their own code. For example, if the request to upload a file is a picture content, the extension is absolutely impossible to judge, but also need to use the characteristics of the image to verify.

The above describes the php file upload file upload, including the contents of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.