dtlab08822 2018-04-04 12:24
浏览 77
已采纳

PHP使用随机名称上传多个文件

I got problem to set file name when uploading the image using multiple input file.

<span class="btn btn-default btn-file">
    Browse <input type="file" name="image[]" id="image" class="image" onchange="preview_image();" multiple/>
</span>

And here is the PHP:

$total = count($_FILES['image']['name']);

for($i=0; $i<$total; $i++)
{
    $tmpFilePath = $_FILES['image']['tmp_name'][$i];

    if($tmpFilePath != "")
    {
        $info1 = pathinfo($_FILES['image']['name'][$i]);
        $ext = $info1['extension'];
        $newname1 = "CALIBRATION_.".$ext; 

        $newFilePath = "../assets/img/product/" . $newname1[$i];

        if(move_uploaded_file($tmpFilePath, $newFilePath)){}
    }
}

When I try to run the code, it show me wrong name & type. You can see below screenshot.

enter image description here

What I want is to rename the file after uploaded with correct name and file type.

  • 写回答

4条回答 默认 最新

  • douliandan7340 2018-04-04 12:28
    关注

    You access $newname1 as an array, this is not what you want. Plus, you are missing the random of your name.

    try doing

    $total = count($_FILES['image']['name']);
    
    for($i=0; $i<$total; $i++)
    {
        $tmpFilePath = $_FILES['image']['tmp_name'][$i];
    
        if($tmpFilePath != "")
        {
            $info1 = pathinfo($_FILES['image']['name'][$i]);
            $ext = $info1['extension'];
            $newname1 = "CALIBRATION_" . rand(10000,99999) . "." . $ext; 
    
            $newFilePath = "../assets/img/product/" . $newname1;
    
            if(move_uploaded_file($tmpFilePath, $newFilePath)){}
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题