PHP uploads multiple files

Source: Internet
Author: User

index.php Page Form page

<form action= "test.php" method= "post" enctype= "Multipart/form-data" > Name:<input type= "text" name= "username "Value=" "/><br/> <input type=" hidden "name=" max_file_size "value=" 1000000 "/> Up pic:<input type= "File" name= "picture[" "value=" "/><br/> up pic:<input type=" file "Name=" picture[] "value=" "/><br/&    Gt Up pic:<input type= "file" Name= "picture[]" value= ""/><br/> <input type= "Submit" value= "Upload"/>< ;br/> </form>

test.php page processing Submission Information page

<?php header (' Content-type:text/html;charset=utf-8 ');//var_dump ($_post);//var_dump ($_FILES);/* *   Single File Upload  * array  (size=2)    ' username '  => string  ' yang '   ( length=4)    ' max_file_size '  => string  ' 1000000 '   (length=7) array  (size=1)    ' picture '  =>     array  (size=5)         ' name '  => string  ' php.jpg '   (length=7)        ' Type '  => string  ' image/jpeg '   (length=10)        ' Tmp_name '  => string  ' D:\wamp\tmp\php7C.tmp '   (length=21)        ' Error '  => int 0       ' size '  => int 279468            * * multiple file uploads  *array  (size=2)    ' username '  => string  ' Xiaohua '   (length=7)    ' max_file_size '  => string  ' 1000000 '   (length=7) array  (size=1)    ' picture '  =>     array  ( size=5)        ' name '  =>          array  (size=3)           0 => string   ' php.jpg '   (length=7)           1 =>  string  ' php.jpg '   (length=7)           2 =>  string  ' php.jpg '   (length=7)        ' type '  =>          array  (size=3)            0 => string  ' Image/jpeg '   (length=10)            1 => string  ' Image/jpeg '   (length=10)            2 => string  ' Image/jpeg '   (length=10)        ' Tmp_name '  =>         array  (size=3)            0 => string  ' D:\wamp\tmp\php89.tmp '   ( length=21)           1 => string  ' D:\wamp\ Tmp\php8a.tmp '   (length=21)           2 =>  string  ' D:\wamp\tmp\php8B.tmp '   (length=21)        ' ERROR '  =>          array  (size=3)            0 => int 0           1 => int 0          2 => int 0        ' Size '  =>         array  (size=3)           0 => int 279468           1 => int 279468           2 => int 279468  * */  $num  =  count ($_files[' picture ' [' name ']); for ($i =0; $i < $num; $i + +) {//Set multiple file uploads     //first step: judging errors     if ($_files[' picture '] [' ERROR '] [$i]>0) {         switch  ($_files[' picture '] [' ERROR '] [$i]) {             case 1 :                 echo  ' onThe file passed exceeds the value of the  upload_max_filesize  option limit in  php.ini . ';                break;             case 2 :                 echo  ' upload file size exceeds  HTML  The value specified by the  MAX_FILE_SIZE  option in the form.   ';                break;             case 3 :                 echo  ' files are only partially uploaded. ';                break;             case 4 :                 echo  ' no files were uploaded. ';                break;             case 6 :                 echo  ' could not find the Temp folder. ';                break;             case 7 :                 echo  ' File write failed. ';                break;             default:                 echo  ' unknown error '; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp;  }        //exit;         continue;    }        //Second Step   judging type      $arr  = explode (".", basename ($_files[' picture '] [' name '] [$i]));          $ext  = array_pop ($arr);          $allowType  = array ("gif", "png", "JPG", "jpeg");     if (!in_array ($ext,   $allowType)) {            echo  ' The type of upload is not legal ';            exit;    }         //Step three   determine size      $maxsize  = 1000000;     if ($_files[' picture '] [' size '] [$i]> $maxsize) {             echo  "The uploaded file exceeds the maximum storage value {$maxsize} bytes";             exit;    }        //Fourth Step   file name after uploading   Be sure to set the random file name          $tmpfile  = $_files[' picture ' [' Tmp_ Name '] [$i];      $destname  =  "./uploads/". Date (' Ymdhis '). Rand (99,99999). ".". $ext;//Set the random file name    //the uploaded file in the temporary directory to the specified new directory, even if the upload is successful.    if (Move_uploaded_file ($tmpfile,  $destname)) {        echo  "uploaded successfully! ";   }else{       echo " Upload failed! ";    }   }



This article is from the "Jin Sha Harbor" blog, please be sure to keep this source http://11410485.blog.51cto.com/11400485/1841610

PHP uploads multiple files

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.