PHP multi-File upload operation, _php tutorial

Source: Internet
Author: User
Tags php file upload

PHP multi-File upload operation,


In the previous article about the PHP file upload principle and simple operation example is a single file upload.

Http://www.cnblogs.com/lichenwei/p/3879566.html

In fact, multi-file upload and single-file upload is similar, the principle is the same, just a bit of code to do a little tricks.

First or index.html upload the form, just to change the file in the form of the previous upload to file[]

  DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "><HTMLxmlns= "http://www.w3.org/1999/xhtml"Xml:lang= "en"><Head>    <Metahttp-equiv= "Content-type"content= "Text/html;charset=utf-8">    <title>Upload files
   title>
    Head><Body>    <formAction= "upload.php"enctype= "Multipart/form-data"Method= "POST">        <inputtype= "hidden"name= "Max_file_size"value= "100000" />Upload file:<inputtype= "File"name= "file[]"/><BR/>Upload file:<inputtype= "File"name= "file[]"/><BR/>Upload file:<inputtype= "File"name= "file[]"/><BR/>            <inputtype= "Submit"value= "Upload" />     
       form> 
        body> 
         HTML >   
 

Print a look at upload.php with $_files

 
  Php
Print_r ($_files
?>

The following multidimensional arrays are derived

Array(    [file] =Array([name]=Array                (                    [0] = photo 1.jpg [1] = Photo 2.jpg [2] = Photo 3.jpg) [Type]=Array                (                    [0] = image/JPEG [1] = image/JPEG [2] = image/jpeg) [Tmp_name]=Array                (                    [0] = F:\wamp\tmp\php36C7.tmp [1] = F:\wamp\tmp\php36C8.tmp [2] = F:\wamp\tmp\php36C9.tmp) [ERROR]=Array                (                    [0] = 0                    [1] = 0                    [2] = 0) [Size]=Array                (                    [0] = 0                    [1] = 0                    [2] = 0                )        ))

According to the principle of single file upload, first think about what we need to get?

Obviously we need to get an array of information about the file, the array contains Name,type,tmp_name,error,size, and at this point we get a multidimensional array, although the corresponding key value exists, but it is multidimensional,

We just need to split it, like the above 3 files, we just need to split it into the corresponding 3 file information array on the line.

Structure of a split fraction group

  Array   ([ 0] = =  Array   ([name] => photo 1.  jpg [type]  = image/ jpeg [tmp_name]  = = F:\wamp\tmp\php13C1.   tmp [ERROR]  = 0  [size]  = + 385150 ) [ 1] = =  Array   ([name]  = Photo 2.  jpg [type]  = image/ jpeg [tmp_name]  = = F:\wamp\tmp\php13D2.   tmp [ERROR]  = 0  [size]  = + 242043 ) [ 2] = =  Array   ([name]  = Photo 3.  jpg [type]  = image/ jpeg [tmp_name]  = = F:\wamp\tmp\php13D3.  tmp [ERROR]  = 0  [size]  = = 488293 ))  

The following is a split-up of the reorganized array code

 
  PHP        //print_r ($_files[' file ');    $arr=$_files[' file '];     $files=array();      for ($i= 0; $i<count($arr[' name ']); $i+ +)        {$files[$i] [' name ']=$arr[' name '] [$i ];         $files [$i] [' type ']=$arr[' type '] [$i]        ; $files [$i] [' Tmp_name ']=$arr[' tmp_name '] [$i]        ; $files [$i] [' Error ']=$arr[' Error '] [$i]        ; $files [$i] [' Size ']=$arr[' Size '] [$i]    ;    } Print_r ($files);
?>

The rest of the stuff is simple, repeat the steps of the single file upload, and iterate over the array on the line.

The code is as follows:

 Php//print_r ($_files[' file ');    $arr=$_files[' File ']; $files=Array();  for($i= 0;$i<Count($arr[' name ']);$i++){//count () Statistics array key value name length        $files[$i[' Name ']=$arr[' Name '] [$i]; $files[$i[' Type ']=$arr[' type '] [$i]; $files[$i[' Tmp_name ']=$arr[' Tmp_name '] [$i]; $files[$i[' Error ']=$arr[' ERROR '] [$i]; $files[$i[' Size ']=$arr[' Size '] [$i]; }     for($i= 0;$i<Count($files);$i++){    //Get upload file information    $fileName=$files[$i[' Name ']; $fileType=$files[$i[' Type ']; $fileError=$files[$i[' Type ']; $fileSize=$files[$i[' Size ']; $tempName=$files[$i[' Tmp_name '];//temp file name//definition upload file type    $typeList=Array("Image/jpeg", "image/jpg", "Image/png", "image/gif");//define the allowed types    if($fileError>0){            //Upload file error number judgment            Switch($fileError) {                 Case1:$message= "The uploaded file exceeds the value of the Upload_max_filesize option limit in php.ini. ";  Break;  Case2:$message= "The size of the uploaded file exceeds the value specified by the Max_file_size option in the HTML form." ";  Break;  Case3:$message= "The file is only partially uploaded. ";  Break;  Case4:$message= "No files were uploaded." ";  Break;  Case6:$message= "The Temp folder could not be found." ";  Break;  Case7:$message= "File Write Failed";  Break;  Case8:$message= "File upload interrupted due to PHP extension";  Break; }            Exit("File Upload failed:".)$message); }    if(!Is_uploaded_file($tempName)){        //determine if it is a post-uploaded file.        Exit("not uploaded by HTTP POST"); }Else{        if(!In_array($fileType,$typeList)){            Exit("uploaded file is not a specified type"); }Else{            if(!getimagesize($tempName)){                //prevent users from uploading malicious files, such as changing the virus file name extension to picture format                Exit("The uploaded file is not a picture"); }        }            if($fileSize>1000000){                //limit the size of upload files for specific forms                Exit("Upload file exceeds the limit size"); }Else{                //avoid uploading files in Chinese name garbled                $fileName=Iconv("UTF-8", "GBK",$fileName);//Convert the character encoding Iconv crawled from utf-8 to GBK output                $fileName=Str_replace(".", Time().".",$fileName);//Add a timestamp after the name of the image to avoid overwriting files with duplicate names                if(Move_uploaded_file($tempName, "uploads/".$fileName)){                    Echo"Upload the file successfully!" "; }Else{                    Echo"Upload file Failed"; }            }        }    }?>

The effect is as follows:




http://www.bkjia.com/PHPjc/853717.html www.bkjia.com true http://www.bkjia.com/PHPjc/853717.html techarticle PHP Multi-File upload operation, in the previous article about the PHP file upload principle and simple operation example is a single file upload. Http://www.cnblogs.com/lichenwei/p/3879566.html actually ...

  • Related Article

    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.