我目前正在尝试使用一种表单(AJAX)和Bulletproof library上传多个图像文件。
我设法获得1个工作,#avatar-upload-input
通过AJAX表单成功上传了图像。
<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>
现在我正在尝试使第二个工作,但是我真的不确定如何去使它工作!
我用Google搜索它,看到了more people were having the same question,但没有一个真正的答案。
在这些问题的评论中建议在$ _FILES上需要循环。我尝试了以下方法:
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\utils\resize($upload_pfp->getFullPath(), $upload_pfp->getMime(), $upload_pfp->getWidth(), $upload_pfp->getHeight(), 190, 175);
}
}
那没有给我任何错误,但是也没有上传任何图像。
我希望有人能帮助我解决如何使用上述库上传多张图片!
答案 0 :(得分:2)
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\utils\resize( //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
}
}
}