文件未在文件阅读器中上传

时间:2016-06-08 10:28:11

标签: javascript php filereader

我正在使用File Reader来选择和上传图片。 当我点击提交按钮时,我的图像被选中预览但没有上传。

在upload.php中,$ _FILES的输出是一个空数组。

问题在哪里?

HTML

<form action="upload.php" method="post" enctype="multipart/form-data">
<div id="wrapper" style="margin-top: 20px;">
    <input id="fileUpload" multiple="multiple" type="file"/> 
  <input type="submit" value="Upload Image" name="upload">

    <div id="image-holder">

    </div>
</div>
</form>

的Javascript

<script>

$(document).ready(function() {
        $("#fileUpload").on('change', function() {
          //Get count of selected files
          var countFiles = $(this)[0].files.length;
          var imgPath = $(this)[0].value;
          var extn = imgPath.substring(imgPath.lastIndexOf('.') + 1).toLowerCase();
          var image_holder = $("#image-holder");
          image_holder.empty();
          if (extn == "gif" || extn == "png" || extn == "jpg" || extn == "jpeg") {
            if (typeof(FileReader) != "undefined") {
              //loop for each file selected for uploaded.
              for (var i = 0; i < countFiles; i++) 
              {
                var reader = new FileReader();
                reader.onload = function(e) {
                  $("<img />", {
                    "src": e.target.result,
                    "class": "thumb-image"
                  }).appendTo(image_holder);
                }
                image_holder.show();
                reader.readAsDataURL($(this)[0].files[i]);
              }
            } else {
              alert("This browser does not support FileReader.");
            }
          } else {
            alert("Pls select only images");
          }
        });
      });
</script>

upload.php的

<?php

require_once './include/db_connection.php';
if(isset($_POST['upload']))
{
print_r($_FILES);
die();

if(!empty($_FILES)){

    $targetDir = "upload/";
    $fileName = $_FILES['file']['name'];
    $targetFile = $targetDir.$fileName;

   if(move_uploaded_file($_FILES['file']['tmp_name'],$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';
    }

}
else
{
    echo 'No file selected';
}
}

1 个答案:

答案 0 :(得分:1)

您的文件输入没有name

表单控件的name用于确定它在提交的数据中将具有哪个键。没有它们的控件将不会成功(即根本不会出现在提交的数据中)。