PHP实现多文件上传


submit.html

<html>
<head>
    <meta charset="utf-8">
    <title>文件上传</title>
</head>
<body>

    <h1 style="color:white;background-color:#525D76;font-size:22px;text-align: center">文件上传</h1>
    <form action="upload.php" method="post" enctype="multipart/form-data">     <!--ENCTYPE="multipart/form-data"用于表单里有图片上传-->
    <input type="hidden" name="max_file_size" value="1000000"/>
    <table border="0">
    <tr>
        <th align="right" valign="top">上传文件:</th>
        <td>    
            <input type="file" name="fl[]" size:"60"><br/>   <!--可以将多个变量作为一个变量取得,可以使用"名称[]",这样变量的值就会以数组的形式被赋值-->
            <input type="file" name="fl[]" size:"60"><br/>
            <input type="file" name="fl[]" size:"60"><br/>

            <input type="checkbox" name="forbid" value="true"/>是否覆盖同名文件<br/>
            <input type="submit" name="" value="上传"/>
        </td>
    </tr>
    </table>
    </form>
</body>
</html>

upload.php


<html>
<head>
    <meta charset="utf-8">
    <title>上传结果</title>
</head>
<body>
    <h1 style="color:white;background-color:#525D76;font-size:22px;text-align: center">上传结果</h1>
    <table border='1' width="350">
    <tr>
    <th>文件名</th><th>大小</th><th>MINE类型</th>
    </tr>

    <?php
    $path='./File/';//上传的地址
    $num=0;

    //求出文件个数,一个一个处理

    for ($i=0; $i < sizeof($_FILES['fl']['name']); $i++) { 
        $name = mb_convert_encoding($_FILES['fl']['name'][$i],'GB2312','UTF-8');  //将文件的文字码进行转换

        if($_FILES['fl']['name'][$i] == ''){
            continue;
        }//文件为空时,跳到下一个文件

        if(file_exists($path.$name) == TRUE&&$_POST['forbid'] == 'true'){
            $num++;
        }elseif (!is_uploaded_file($_FILES['fl']['tmp_name'][$i])) {
            $num++;
        }else{
    ?>
        <tr>
            <td align="right"><?php print($_FILES['fl']['name'][$i]);?></td>
            <td align="right"><?php print($_FILES['fl']['size'][$i]);?>byte</td>
            <td align="right"><?php print($_FILES['fl']['type'][$i]);?></td>
        </tr>
        <?php
            move_uploaded_file($_FILES['fl']['tmp_name'][$i],$path.$name);
        }
    }
    if($num>0){
        print('<div style="color:red">'.$num.'件上传失败</div>');
    }
    ?>
    </table>
</body>
</html>
博客园:https://www.cnblogs.com/newtol 微信公众号:Newtol 【转发请务必保留原作者,否则保留追责权利】
原文地址:https://www.cnblogs.com/newtol/p/10159138.html