首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

PHP 多圖片上傳驗證完整實現:類型檢查、重名處理、結果反饋

     編輯:PHP 綜合知識

核心需求

  • 支持多文件同時上傳(示例限制 5 張)
  • 嚴格驗證圖片類型(防偽造 Content-Type)
  • 重名文件檢測與處理
  • 詳細上傳結果報表

核心算法

使用 $_FILES['imgfile']['name'][$i] 數組結構遍歷處理每個文件。

完整代碼實現

<?php
$img_dir = "../upload/";  // 上傳目錄(需寫權限)

// 初始化統計
$uploaded = 0;
$unuploaded = 0;
$result = array();

// 遍歷最多 5 個文件
for ($i = 0; $i <= 5; $i++) {
    $is_file = $_FILES['imgfile']['name'][$i];
    if (empty($is_file)) continue;

    $result[$i] = "<tr class='td2' align='center'>
        <td>{$_FILES['imgfile']['name'][$i]}</td>
        <td>" . round($_FILES['imgfile']['size'][$i]/1024, 2) . "K</td>
        <td>{$_FILES['imgfile']['type'][$i]}</td>
        <td>";

    // 類型白名單驗證
    $allowedTypes = array(
        'image/pjpeg',   // JPG (IE)
        'image/jpeg',    // JPG (標準)
        'image/gif',
        'image/png',
        'image/x-png',   // PNG (IE)
        'image/bmp'
    );
    
    if (in_array($_FILES['imgfile']['type'][$i], $allowedTypes)) {
        // 重名檢測
        $targetPath = $img_dir . $_FILES['imgfile']['name'][$i];
        if (!file_exists($targetPath)) {
            if (move_uploaded_file($_FILES['imgfile']['tmp_name'][$i], $targetPath)) {
                $result[$i] .= "成功";
                $uploaded++;
            } else {
                $result[$i] .= "<font color='red'>移動失敗</font>";
                $unuploaded++;
            }
        } else {
            $result[$i] .= "<font color='red'>文件已存在</font>";
            $unuploaded++;
        }
    } else {
        $result[$i] .= "<font color='red'>類型不允許</font>";
        $unuploaded++;
    }
    
    $result[$i] .= "</td></tr>";
}

// 空提交檢查
if (empty($result)) {
    die("錯誤:未選擇任何圖片");
}

// 輸出結果表格
echo "<table cellpadding=4 cellspacing=1 border=0 class=table width=400 align=left>
    <tr class=navi align=center>
        <td>文件名</td><td>大小</td><td>類型</td><td>上傳結果</td>
    </tr>";
foreach ($result as $value) echo $value;
echo "<tr class=td1>
    <td colspan=4>共上傳 " . ($uploaded + $unuploaded) . ", 成功: $uploaded, 失敗:<font color=red> $unuploaded</font> </td>
</tr>
<tr class=navi>
    <td colspan=4 align=center>[ <a href='uploadimg.php?action=upload'>繼續上傳</a> ] &nbsp; [ <a href='image.php'>瀏覽圖片</a> ]</td>
</tr>
</table>";
?>

安全加固建議(生產環境必做)

  1. 雙重驗證:前端 JS 攔截 + 後端 PHP 驗證(getimagesize() 驗證真實圖片頭)
  2. 文件重命名:uniqid() . '.' . pathinfo($name, PATHINFO_EXTENSION) 避免重名與惡意文件名
  3. 存儲隔離:上傳目錄禁用腳本執行權限(php_flag engine off)
  4. 大小限制:php.ini upload_max_filesize + 代碼層二次檢查
  5. 病毒掃描:集成 ClamAV 等掃描引擎

HTML 表單模板

<form action="uploadimg.php" method="post" enctype="multipart/form-data">
    <input type="file" name="imgfile[]" /><br>
    <input type="file" name="imgfile[]" /><br>
    <input type="file" name="imgfile[]" /><br>
    <input type="file" name="imgfile[]" /><br>
    <input type="file" name="imgfile[]" /><br>
    <input type="submit" value="上傳" />
</form>
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved