【问题标题】:How to iterate throught an input type="file" name='Image[]' multiple如何遍历输入 type=\"file\" name=\'Image[]\' multiple
【发布时间】:2023-01-13 13:06:28
【问题描述】:

大家好,这是 vineet 我是 php 的新手,有一种情况需要在数组中接收输入类型 =“文件”的值,但我无法获取数据,因为它给我显示未定义数组键的错误

 html code



<input type="file" class="inputImage"  name="CrewImage[]" accept="image/png , image/jpg , image/jpeg" required>




 phpcode
   

 <?php
      $crewImage=$_POST['CrewImage'];
    ?>

我想访问这个的单个值,以便我可以将这个值保存在数据库中

我试过这个

for($i=0;$i< count($crewImage);$i++){
       
        $MovieCastImage=addslashes(file_get_contents($_FILES[ $_POST['CrewImage['.$i.']']]['tmp_name']));
       echo $MovieCastImage;
  
    }

但这也会给出错误 Undefined array key

【问题讨论】:

    标签: php laravel web-development-server


    【解决方案1】:

    您可以使用元素的 files 属性来遍历用户选择的多个文件。 files 属性是一个 FileList 对象,其中包含所有选定的文件。

    以下是如何使用 for 循环遍历文件的示例:

    const input = document.querySelector("input[type='file'][name='Image[]']");
    
    for (let i = 0; i < input.files.length; i++) {
        const file = input.files[i];
        // do something with the file
    }
    

    或者,您可以使用 FileList 对象的 forEach 方法:

    const input = document.querySelector("input[type='file'][name='Image[]']");
    
    input.files.forEach(function (file) {
        // do something with the file
    });
    

    您还可以使用 for-of 循​​环来迭代文件

    const input = document.querySelector("input[type='file'][name='Image[]']");
    
    for (const file of input.files) {
        // do something with the file
    }
    

    在所有情况下,您都可以使用文件对象访问每个文件的属性,例如名称、大小和类型。如有必要,您还可以使用 FileReader API 读取文件的内容。

    请务必注意,输入元素必须在脚本运行之前位于 DOM 中才能访问文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-12
      • 1970-01-01
      • 2012-10-02
      • 1970-01-01
      • 1970-01-01
      • 2015-10-10
      相关资源
      最近更新 更多