繁体   English   中英

多张图片上传问题

[英]Multiple images uploading issue

我正在尝试在数据库中插入多个图像。 提交后,我在$ _FILES []中仅获得一张图像详细信息。 为什么其他提交的图像未按阵列显示?

另外,upload.php在第15行的C:\\ xampp \\ htdocs \\ kabootar \\ upload.php中有错误说数组到字符串转换是

$targetFile = $targetDir.$fileName; 

我该如何解决这些问题?

html

    <form action="upload.php" method="post" enctype="multipart/form-data">
        <input type="submit" value="Upload" name="upload">

        <label class="add">Select more..</label>
         <div class="prep">

        <div class="col-md-4">
            <img src="http://png-4.findicons.com/files/icons/129/soft_scraps/256/button_upload_01.png" id="upfile1" style="cursor:pointer" class="img" />
   <input type="file" class="inputimg" name="multiple_uploaded_files[]" />
        </div>


         </div>

    </form> 

javascript

<script type="text/javascript">
$(document).on("click", ".img", function () {

    $(this).closest("div").find(".inputimg").trigger("click");
});

var count = 1;
$(".add").on("click", function () {
    count++;
    if(count <= 5){
        var row = '<div class="col-md-4"> <img src="http://png-4.findicons.com/files/icons/129/soft_scraps/256/button_upload_01.png" id="upfile1" style="cursor:pointer" class="img"/ ><input type="file" class="inputimg" />\n\
       ';
    $(".prep").append(row);

    $(".inputimg").change(function () {
        console.log(this);
        readURL(this);
    });
    }else{
        alert('You are only allowed to add uptp 5 images');
    }


});
$(".inputimg").change(function () {
    readURL(this);
});

function readURL(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
            $(input).siblings('.img').attr('src', e.target.result);
        }

        reader.readAsDataURL(input.files[0]);
    }
}
</script>

upload.php

if(isset($_POST['upload']))
{

if(!empty($_FILES)){

  foreach ($_FILES['multiple_uploaded_files']['name'] as $file)
 {
    $targetDir = "upload/";
    $fileName = $_FILES['multiple_uploaded_files']['name'];   
    $targetFile = $targetDir.$fileName; 
        //use the move_uploaded_file() to move your file on your server directory.
if(move_uploaded_file($_FILES['multiple_uploaded_files']['tmp_name'][$file], $targetFile))
{
        //insert file information into db table
 $sql = mysqli_query($link,"INSERT INTO files (file_name, uploaded) VALUES('".$fileName."','".date("Y-m-d H:i:s")."')");
 echo 'file inserted';  
 }
    else
    {
        echo 'Query not working';
    }

}
        //fire an insert query that inserts all the file names with comma separated value
    }
else
{
    echo 'No file selected';
}

您正在尝试使用数组$ _FILES ['multiple_uploaded_files'] ['name']连接字符串。 对于数组到字符串的转换错误

替换代码

$fileName = $_FILES['multiple_uploaded_files']['name'];   

$fileName = $file;   (or)
$fileName = $_FILES['multiple_uploaded_files']['name'][0];   

单个文件上传问题。 您尚未在javascript代码中为输入标签添加名称属性。

更换

    var row = '<div class="col-md-4"> <img src="http://png-4.findicons.com/files/icons/129/soft_scraps/256/button_upload_01.png" id="upfile1" style="cursor:pointer" class="img"/ ><input type="file" class="inputimg" />\n\';

    var row = '<div class="col-md-4"> <img src="http://png-4.findicons.com/files/icons/129/soft_scraps/256/button_upload_01.png" id="upfile1" style="cursor:pointer" class="img"/ ><input type="file" class="inputimg" name="multiple_uploaded_files[]"/>\n\';

注意:在输入标签中添加了名称属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM