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 matlab数字图像处理频率域滤波
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行