dongzha0149 2019-04-20 12:57
浏览 306
已采纳

如何将html表单中的数组发送到php

I want to send uploaded files address from html to php with array and then moved the uploaded files to images from tmp and checked their size

<form method="post" enctype="multipart/form-data" name="form1" id="form1" action="upload.php">
  <p>

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

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

    <input type="file" name="Img[]" />
  </p>
  <p>
    <input type="submit" name="submit" id="submit" value="Submit">
  </p>
</form>
<?php
$name=rand('0123456789',5).'jpg';
$files=array($_POST['Img[]']);

echo '<pre>';
print_r($files);

if($_FILES['$files[0]']['type'] == 'image/pjpeg'){

    move_uploaded_file($_FILES[$files[]]['tmp_name'],'image/'.$name);
    echo "success";
}
?>

I want to upload files with form

  • 写回答

2条回答 默认 最新

  • douduan5086 2019-04-20 13:38
    关注

    Input file elements never store in the $_POST array. You have to loop through the $_FILES array to get the file elements

    if(isset($_POST['submit']))
      {
    
        for ($i=0; $i < count($_FILES['Img']['tmp_name']) ; $i++) {
          $name=rand('0123456789',5);
          if($_FILES['Img']['type'][$i]  == 'image/jpeg')
          {
            move_uploaded_file($_FILES['Img']['tmp_name'][$i],'image/'.$name . '.jpg');
          }
          echo "success";
        }
    }
    

    In this solution you can increase the number of input files with the same name as Img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • duanchuopi2298 2019-04-20 15:39
    关注

    Try this

    <?php
    if(isset($_POST['submit']))
      {
    
        foreach($_FILES['Img'] as $Img) {
          $name=rand('0123456789',5);
          if($Img['type']  == 'image/jpeg')
          {
            move_uploaded_file($Img['tmp_name'],'image/'.$name . '.jpg');
          }
          echo "success";
        }
    }
    ?>
    
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 python 3des pyDes库
  • ¥15 关于#mysql#安装失败的问题。MySQL
  • ¥15 想问一下for循环计算表达式的方法,第一次接触
  • ¥15 如何在VA框架上面加功能,去读取框架内任何app数据功能
  • ¥15 关于#c语言#的问题:用c或c++写一个计算下列问题有关软件工程的代码并加上分析
  • ¥15 Zeppelin0.10.0版本升级lib包下的shiro-web
  • ¥15 链表入队的指针内存问题
  • ¥20 vba如何写本地html文件执行js
  • ¥15 VS2022的C#如何创建
  • ¥20 关于#用户注册#的问题,如何解决?