首页 > 解决方案 > 在一组上传的文件上应用 php 代码

问题描述

我有一个 php 代码,可以从用户上传的 .dat 文件中提取一些信息。现在我想对一组上传的文件应用这种处理。所以用户可以在点击上传时选择一组文件,必须对每个文件进行处理。我已经尝试过了,但它不起作用:

     <form method="post" enctype="multipart/form-data">
      <label>Selectionner le fichier .dat</label>
      <input type="file" name="excel" />
      <br />
      <input type="submit" name="import" class="btn btn-info" value="Import" />
     </form>

先感谢您。

标签: phphtml

解决方案


要让用户选择多个文件,您必须使用该multiple属性并将括号添加[]到输入名称。

你可以这样做:

<form method="post" enctype="multipart/form-data">
      <label>Selectionner le fichier .dat</label>
      <input type="file" name="excel[]" multiple />
      <br />
      <input type="submit" name="import" class="btn btn-info" value="Import" />
</form>

然后你可以解析$_FILES数组,你会发现所有上传的文件,例如:

Array
(
    [excel] => Array
        (
            [name] => Array
                (
                    [0] => img1.PNG
                    [1] => img2.PNG
                    [2] => img3.jpg
                    [3] => img4.png
                )

            [type] => Array
                (
                    [0] => image/png
                    [1] => image/png
                    [2] => image/jpeg
                    [3] => image/png
                )

            [tmp_name] => Array
                (
                    [0] => /tmp/phpWh5NRz
                    [1] => /tmp/php6KEOer
                    [2] => /tmp/phprLvUBi
                    [3] => /tmp/phplTP0Y9
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                    [3] => 0
                )

            [size] => Array
                (
                    [0] => 63515
                    [1] => 54484
                    [2] => 14001
                    [3] => 84938
                )

        )

)

推荐阅读