dongshuql24533 2014-12-27 09:49
浏览 81
已采纳

问题上传多个同名文件

I have a multiple file upload form in PHP. My issue is when two images of the same name are uploaded the second image is replaced by the earlier one. What can I do? This is my code:

HTML

<form action="" enctype="multipart/form-data" method="post">

    <div>
        <label for='upload'>Add Attachments:</label>
        <input id='upload' name="upload[]" onChange="read_url(this)" type="file" multiple="multiple" id="imgInp" />
    </div>

    <p><input type="submit" name="submit" value="Submit"></p>

</form>

PHP CODE

if(isset($_POST['submit'])){
    if(count($_FILES['upload']['name']) > 0){
        //Loop through each file
        for($i=0; $i<count($_FILES['upload']['name']); $i++) {
          //Get the temp file path
            $tmpFilePath = $_FILES['upload']['tmp_name'][$i];

            //Make sure we have a filepath
            if($tmpFilePath != ""){

                //save the filename
                $shortname = $_FILES['upload']['name'][$i];

                //save the url and the file
                $filePath = "store/".$_FILES['upload']['name'][$i];

                //Upload the file into the temp dir
                if(move_uploaded_file($tmpFilePath, $filePath)) {

                    $files[] = $shortname;

                    $name = 'Admin';

                    $qry="INSERT INTO test(Name,Image) VALUES('$name','$filePath')";

                    mysqli_query($link,$qry) or die(mysqli_error($link));




                    //use $shortname for the filename
                    //use $filePath for the relative url to the file

                }   
            }
            echo '<p style="color:#000;">SUCCESS!!</p>';

        }
    }

Suggest me....

  • 写回答

1条回答 默认 最新

  • douou0977 2014-12-27 09:54
    关注

    you can rename the the filename of each file. simplest way of doing it is to concat with it for example

    $filename = $_FILES['myfilename']['name'];
    $filename = time().$filename;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥500 求华为P30PRO手机硬盘数据恢复
  • ¥15 关于#vscode#的问题:ESP32开发板对接MQTT实现小灯泡的开关
  • ¥15 TMC2209串口模式下读取不到寄存器的值串口助手蓝色字体是发过去的消息,绿色字体是收到的消息,第二行发送读取寄存器的指令但是没有读取到寄存器的值串口助手如下图:接线如下图,如何解决?
  • ¥15 高通安卓11提取完整线刷包软件,或者优博讯dt50顺丰刷机包
  • ¥20 C,有个译码器,换了信道就跑不出原来数据
  • ¥15 MIMIC数据库安装问题
  • ¥60 基于JTag协议开发Fpga下载器上位机,哪位大🐂有偿指导?
  • ¥20 全书网Java爬取数据
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题