PHP上傳多個(gè)文件

index.php頁面表單頁面

創(chuàng)新互聯(lián)是一家專業(yè)提供貢山企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站建設(shè)、做網(wǎng)站H5建站、小程序制作等業(yè)務(wù)。10年已為貢山眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進(jìn)行中。

    <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/>
    up pic:<input type="file" name="picture[]" value=""/><br/>
    <input type="submit" value="upload" /><br/>
    </form>

test.php頁面 處理提交信息頁面

<?php 
header('content-type:text/html;charset=utf-8');
//var_dump($_POST);
//var_dump($_FILES);
/*
 * 單個(gè)文件上傳
 * 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 'p_w_picpath/jpeg' (length=10)
      'tmp_name' => string 'D:\wamp\tmp\php7C.tmp' (length=21)
      'error' => int 0
      'size' => int 279468    
      
 *
 *多個(gè)文件上傳
 *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 'p_w_picpath/jpeg' (length=10)
          1 => string 'p_w_picpath/jpeg' (length=10)
          2 => string 'p_w_picpath/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++){//設(shè)置多個(gè)文件上傳
    //第一步:判斷錯(cuò)誤
    if($_FILES['picture']['error'][$i]>0){
        switch ($_FILES['picture']['error'][$i]){
            case 1 :
                echo '上傳的文件超過了 php.ini 中 upload_max_filesize 選項(xiàng)限制的值。';
                break;
            case 2 :
                echo '上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項(xiàng)指定的值。 ';
                break;
            case 3 :
                echo '文件只有部分被上傳。';
                break;
            case 4 :
                echo '沒有文件被上傳。';
                break;
            case 6 :
                echo '找不到臨時(shí)文件夾。';
                break;
            case 7 :
                echo '文件寫入失敗。';
                break;
            default:
                echo '未知錯(cuò)誤';
        }
        //exit;
        continue;
    }
    
    //第二步 判斷類型
    $arr = explode(".",basename($_FILES['picture']['name'][$i]));    
    $ext = array_pop($arr);    
    $allowType = array("gif","png","jpg","jpeg");
    if(!in_array($ext, $allowType)){
            echo '上傳的類型不合法';
            exit;
    }
    
    //第三步 判斷大小
    $maxsize = 1000000;
    if($_FILES['picture']['size'][$i]>$maxsize){
            echo "上傳的文件超過最大存儲(chǔ)值{$maxsize}字節(jié)";
            exit;
    }
    
    //第四步 上傳后的文件名 一定要設(shè)置隨機(jī)文件名
    
    $tmpfile = $_FILES['picture']['tmp_name'][$i]; 
    $destname = "./uploads/".date('YmdHis').rand(99,99999).".".$ext;//設(shè)置隨機(jī)文件名
   //將臨時(shí)目錄下的上傳的文件,復(fù)制到指定的新目錄,就算上傳成功。
   if(move_uploaded_file($tmpfile, $destname)){
       echo "上傳成功!";
   }else{
       echo "上傳失??!";
   }
   
}

網(wǎng)站標(biāo)題:PHP上傳多個(gè)文件
文章源于:http://muchs.cn/article26/ijdojg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、域名注冊(cè)、網(wǎng)站內(nèi)鏈、品牌網(wǎng)站制作、App開發(fā)虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作