PHP上传:上传多个文件? [英] PHP Uploading: Uploading multiple files?

查看:87
本文介绍了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>

并使用以下脚本:

<?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>

但是我不确定如何修改php以处理所有三个文件.我知道我需要遍历_FILES,但是我尝试过的所有方法都无法正常工作.任何指针将不胜感激.

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.

推荐答案

每个文件元素必须具有唯一的名称,或使用PHP的数组速记:

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

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

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

记住-name属性定义了如何在服务器上标识该字段,如果多次使用相同的名称,则在解析提交的数据时,PHP将用最新的副本覆盖以前的副本.数组符号([])告诉PHP您打算拥有多个具有相同名称的字段,并且它找到的每个副本都应添加到数组中,而不要被覆盖.

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.

对于数组版本,PHP具有设计上的愚蠢性,需要稍作不同的处理.最后得到一个看起来像$ p_FILES的数组

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...
         )
    )
)

这篇关于PHP上传:上传多个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆