dongneng5383 2012-01-06 16:29
浏览 19
已采纳

PHP上传:上传多个文件?

I'm currently uploading a single file successfully with the following form:

<html>
<body>

<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /> 

<br />
<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html>

And with the following script:

<?php
error_reporting(E_ALL);



    if (($_FILES["file"]["size"] < 20000))
      {
      if ($_FILES["file"]["error"] > 0)
        {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        }
      else
        {
        echo "Upload: " . $_FILES["file"]["name"] . "<br />";
        echo "Type: " . $_FILES["file"]["type"] . "<br />";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";


        $moved = move_uploaded_file($_FILES["file"]["tmp_name"], "C:/inetpub/wwwroot/PHP_Ramp/upload/" . $_FILES["file"]["name"]);

        if ($moved) {
            echo "Move: Success <br/>";
        }
        else {
            echo "Move Failed <br/>";
        }


          echo "Stored in: " . "C:/inetpub/wwwroot/PHP_Ramp/upload/" . $_FILES["file"]["name"];
          }
        }

    else
      {
      echo "Invalid file";
      }
    ?>

I'm now trying to allow the user to select three different files from the form. I've found a few guides that show how to do something similar, but I can't quite get it working.

I've modified the form as follows (to include three inputs):

<html>
<body>

<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /> 
<input type="file" name="file" id="file" />
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html>

But I'm not sure how to modify the php to handle all three files. I know I need to iterate through _FILES but everything I've tried isn't working. Any pointers would be appreciated.

  • 写回答

5条回答 默认 最新

  • douhuang3740 2012-01-06 16:33
    关注

    Each file element must have a unique name, or use PHP's array shorthand:

    <input type="file" name="file1" />
    <input type="file" name="file2" />
    

    or

    <input type="file" name="file[]" />
    <input type="file" name="file[]" />
    

    Remember - the name attribute defines how you'll identify that field on the server, and if you use the same name multiple times, PHP will overwrite previous copies with the latest one as it parses through the submitted data. The array notation ([]) tells PHP that you INTENDED to have multiple fields with the same name, and that each copy it finds should be added to an array, not overwritten.

    For the unique name version, you'd handle each as you are right now with the single file.

    For the array version, PHP has a design stupidity that requires slightly different handling. You end up with a $_FILES array that looks like

    $_FILES = array(
        'fieldname' => array(
             'name' => array(
                 0 => 'first file',
                 1 => 'second file',
                 etc...
             )
        )
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常