weixin_33724659 2017-07-01 01:58 采纳率: 0%
浏览 17

AJAX PHP上传文件问题

I am uploading multiple files using add more files in ajax and save it in database in php. But it insert only last file in database not multiple file in database.

HTML code

<form id="registration_form" method="post" enctype="multipart/form-data">
   <input type="file"   id="file" name="file[]">
   <input type="button" id="add_more" class="upload" value="Add More Files"/>
</form>

AJAX code

$(document).ready(function() {
    //  To add new input file field dynamically, on click of "Add More Files" button below function will be executed.
    $('#add_more').click(function() {
    $(this).before($("
    <div id='filediv'/>
    ").append($("<input name='file[]' type='file' id='file'>"
    )));
    $.ajax({
    url:"update.php",
    type:"post",
    data: new FormData(this),
    contentType:false,
    cache:false,
    processData:false,
    success:function(data)
    {
    alert(data);
    },
    failure:function(errormsg)
    {
    alert(errormsg);
    }
    });
});`

PHP code

 <!------- Including PHP Script here ------>
 <?php if (isset($_POST['submit'])) {
   $j = 0;     // Variable for indexing uploaded image.
   $target_path = "upload/";     // Declaring Path for uploaded images.
   for ($i = 0; $i < count($_FILES['file']['name']); $i++) {
   // Loop to get individual element from the array
   $validextensions = array("jpeg", "jpg", "png");      // Extensions which are allowed.
   $ext = explode('.', basename($_FILES['file']['name'][$i]));   // Explode file name from dot(.)
   $file_extension = end($ext); // Store extensions in the variable.
   $target_path = $target_path  . $_FILES['file']['name'][$i];  
   //echo $target_path; die;   // Set the target path with a new name of image.
   $j = $j + 1;      // Increment the number of uploaded images according to the files in array.
   if (($_FILES["file"]["size"][$i] < 800000)     // Approx. 800kb files can be uploaded.
   && in_array($file_extension, $validextensions)) {
   if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {
   // If file moved to uploads folder.
   echo $j. ').<span id="noerror">Image uploaded successfully!.</span><br/><br/>';
   } else {     //  If File Was Not Moved.
   echo $j. ').<span id="error">please try again!.</span><br/><br/>';
   }
   } else {     //   If File Size And File Type Was Incorrect.
   echo $j. ').<span id="error">***Invalid file Size or Type***</span><br/><br/>';
   }
   }
   } ?>
  • 写回答

1条回答 默认 最新

  • larry*wei 2017-07-01 02:43
    关注

    You need to append all the files to form data. Try this

    var data = new FormData();
    
    $.each($("input[type='file']")[0].files, function(i, file) {
        data.append('file', file);
    });
    

    Pass this data variable to ajax data.

    评论

报告相同问题?

悬赏问题

  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图