我目前正在尝试使用单一表单(AJAX)和Bulletproof库上传多个图像文件。
我设法使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上搜索了一下,发现有更多的人在 问同样的问题,但是没有一个人得到明确的答案。
在这些问题的注释中建议在$ _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);
}
}
那没有给我任何错误,但是也没有上传任何图像。
希望有人能帮助我解决如何使用上述库上传多张图片!
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
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句