PHP多个图像输入

时间:2015-09-20 12:42:30

标签: javascript php html html5

我制作的代码每次用户添加图像时都附加另一个图像输入。

看起来像这样:

enter image description here

这是代码:

<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

由于

0 个答案:

没有答案