我制作的代码每次用户添加图像时都附加另一个图像输入。
看起来像这样:
这是代码:
<form action="" method="POST" enctype="multipart/form-data">
<ul class="add_img_ul">
<li class="subimg"><input type="file" class="file" name="images[]" required multiple/><img class="add" src="icons/add.jpg" width="80px" height="80px"/></li>
</ul>
<script>
var inputLocalFont = document.querySelector(".file");
inputLocalFont.addEventListener("change",previewImages,false);
function previewImages(){
var fileList = this.files;
var anyWindow = window.URL || window.webkitURL;
for(var i = 0; i < fileList.length; i++){
var objectUrl = anyWindow.createObjectURL(fileList[i]);
$('.add_img_ul').append('<li class="subimg"><input type="file" class="file" name="images[]"/><img class="add_img" src="' + objectUrl + '" width="80px" height="80px"/></li>');
window.URL.revokeObjectURL(fileList[i]);
}
}
</script>
</form>
我的php代码:
if(isset($_FILES['images'])){
$number_of_file_fields = 0;
$number_of_uploaded_files = 0;
$number_of_moved_files = 0;
$uploaded_files = array();
$upload_directory = 'product_images';
for($i = 0; $i < count($_FILES['images']['name']); $i++){
$number_of_file_fields++;
if($_FILES['images']['name'][$i] != ''){
$number_of_uploaded_files++;
$uploaded_files[] = $_FILES['images']['name'][$i];
if(move_uploaded_file($_FILES['images']['tmp_name'][$i], $upload_directory . $_FILES['images']['name'][$i])){
$number_of_moved_files++;
}
}
}
$name = implode(',', $uploaded_files);
echo $name;
}
图像路径记录存储在$name
当我在$name
中运行我的代码时只有一个值,但是当我在一个输入上添加required multiple
时就是好的。
如何将来自不同输入但具有相同名称的值存储到$name
?
由于