dpdbu24262 2017-05-05 02:59
浏览 34

如何在PHP中一次上传多个图像? [重复]

This question already has an answer here:

this is code of my form page

<form action="gallerycontroller.php" method="post" enctype="multipart/form-data">   
<div>
<label class="desc" id="title4" for="Field4">
  Message
</label>                    
<div>
<textarea id="reply" name="name" spellcheck="true" rows="10" cols="50" tabindex="4"></textarea>                 
                             <!-- Custom icons with callback -->
                            <input type="file" name="file">

                            </div>
                          </div>

                          <div>

                          <div>
                                <div>
                                <input id="saveForm" name="saveForm" type="submit" value="Save">
                            </div>
                            </div>

and this is gallerycontroller page

<?php
include("model.php");
$db = new database;
$name = $_POST['name'];
$file = $_FILES['file'];
$filename = $file['name'];
$filetmp = $file['tmp_name'];
$file_ext = explode('.',$filename);
$file_ext = strtolower(end($file_ext));
$file_name_new = uniqid('',true) .'.'. $file_ext;
$file_destination = 'file/' .$file_name_new;
move_uploaded_file($filetmp, $file_destination);


        $designation=$_POST['principal'];
        $r=$db->insert($name,$file_destination);
        if($r>0)
    {   
            header("Location:upload.php?reply='sucess'");
    }
    else
    {
         header("Location:upload.php?reply='fail'");    
    }
 ?>

but i want to upload multiple files at a time this code of single file upload is working fine . Please convert this code into multi file upload if possible else refer new code.

</div>
  • 写回答

1条回答 默认 最新

  • doucheng7534 2017-05-05 03:03
    关注

    use "multiple" attribute for the file

    <input type="file" name="img[]" multiple>
    

    then use for loop in the php

    $loop = 0;
    foreach($_FILES['file']['name'] as $file){
        $filename = $file['name'][$loop];
        $filetmp = $file['tmp_name'][$loop];
        $file_ext = explode('.',$filename);
        $file_ext = strtolower(end($file_ext));
        $file_name_new = uniqid('',true) .'.'. $file_ext;
        $file_destination = 'file/' .$file_name_new;
        move_uploaded_file($filetmp, $file_destination);
        $loop++;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计