使用php和jquery ajax上传并保存到数据库


Upload and save to database with php and jquery ajax

我是个业余程序员。我正在用php和jquery ajax编写上传和插入到数据库的函数,但它不起作用我的表格

<form>
<input type="file" id='iputfile1' />
</form>

我的jquery脚本

 iputfile1 = $("#iputfile1").val();
jQuery.ajax({
        type:"POST",
        url:"ex.php", //goi toi file ajax.php
            data:"filename"=filename+"&+"&iputfile1="
        +iputfile1,
        success:function(html){
         jQuery("#responseDiv").show();
         jQuery("#responseDiv").html(html);
        }
       });

我的ex.php文件

$iputfile1 = $_REQUEST['iputfile1'];
print_r($iputfile1)

选择文件并提交我的ex.php文件后未收到$_file['tmp']

<input type="file" class="file">

$(".file").on("change",function(){
   var file = new FormData();
   file.append('file',$('.file')[0].files[0]);
   $.ajax({
    url: "upload.php",
    type: "POST",
    data: file,
    processData: false,
    contentType: false, 
     beforeSend:function(){
    $(".result").text("Loading ...");
     }, 
     success:function(data){
      $(".result").html(data);
    }
});

<div class="result"></div>
in upload.php

<?php
include("database.php");
$name = $_FILES["file"]["name"];
if(move_uploaded_file($_FILES["file"]["tmp_name"], "DESTINATION/".$name)){
      // insert to data base
      echo '<img src="DESTINATION/'.$name.'">';
}
?>