dtiu94034 2018-10-03 20:31 采纳率: 0%
浏览 11
已采纳

PHP - 使用防弹库上传多个文件

I'm currently trying to upload multiple image files using a single form (AJAX) and the Bulletproof library.

I managed to get 1 working, the #avatar-upload-input successfully uploads images through the AJAX form.

<form method="post" action="ajax/updateprofile" enctype="multipart/form-data" id="dynamicform" data-func="updateprofile">
     <input type="file" name="avatar" id="avatar-upload-input" accept="image/*"/>
     <input type="file" name="banner" id="banner-upload-input" accept="image/*"/>
</form>

Now I'm trying to get the second one working, but I'm really not sure how I would go and get that working!

I googled it and saw that more people were having the same question, but none really got a clear answer.

It was suggested in the comments of these issues that a loop is needed on $_FILES. I have tried the following:

foreach($_FILES as $file) {
    $image = new Bulletproof\Image($file);
    $image->setName($Hashids->encode($_SESSION['user_id']) . '-' . uniqid()); 
    $image->setMime(array('jpg', 'png', 'jpeg'));
    $image->setLocation('../assets/images/usercontent/pfp');

    if($image['avatar']){
        $upload_pfp = $image->upload(); 

        bulletproof\utilsesize($upload_pfp->getFullPath(), $upload_pfp->getMime(), $upload_pfp->getWidth(), $upload_pfp->getHeight(), 190, 175);
    }
}

That didn't gave me any errors but also didn't upload any image.

I hope someone can help me solve how can I upload multiple images using the mentioned library!

  • 写回答

1条回答 默认 最新

  • droxlzcgnr639823 2018-10-03 22:12
    关注
    foreach($_FILES as $key => $file) { //get upload name: $key
      $image = new Bulletproof\Image($file);
      $image->setName($Hashids->encode($_SESSION['user_id']) . '-' . uniqid()); 
      $image->setMime(array('jpg', 'png', 'jpeg'));
      $image->setLocation('../assets/images/usercontent/pfp');
    
      if($key == 'avatar'){             //which file
        if($image->upload()){           //upload succeed?
          bulletproof\utilsesize(     //you are still playing with $image
            $image->getFullPath(), 
            $image->getMime(), 
            $image->getWidth(), 
            $image->getHeight(), 
            190,
            175
          );
        }
      }elseif($key == 'banner'){        //do it all over again with banner
        if($image->upload()) {
          //do something with banner
        }
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)