dpg2905 2013-08-27 18:47
浏览 63
已采纳

拖放Jquery上传到PHP

I have seen many topics about this problem but none of them got a legit answer or a including PHP file.

I want to make a drag & drop saving tool. The problem is that my files are not getting uploaded to my ftp folder.

I got the following code:

HTML:

<div id="drop_zone">
  <p>Drop Here</p>
</div>

<form enctype="multipart/form-data" id="yourregularuploadformId">
       <input type="file" name="files[]" multiple="multiple">
</form>

JS:

$(function() {
var dropZone = document.getElementById('drop_zone');    
dropZone.addEventListener('dragover', handleDragOver, false);
dropZone.addEventListener('drop', handleFileSelect, false);

etc.... dropping part

function handleFileSelect(evt) {
    evt.stopPropagation();
    evt.preventDefault();

    files = evt.dataTransfer.files; 
    uploadFile(files);

etc... getting file to my method

function uploadFile(droppedFiles){
    // add your files to the regular upload form
   var uploadFormData = new FormData($("#yourregularuploadformId")[0]); 
    if(droppedFiles.length > 0) { // checks if any files were dropped
        for(f = 0; f < droppedFiles.length; f++) { // for-loop for each file dropped
            uploadFormData.append("files[]",droppedFiles[f]);  // adding every file to the form so you could upload multiple files
        }
    }
 // the final ajax call
    alert(uploadFormData);
       $.ajax({
        url : "php/uploadFile.php", // use your target
        type : "POST",
        data : uploadFormData,
        cache : false,
        contentType : false,
        processData : false,
        success : function(ret) {
                 alert(ret);
        }
       });
  }

Got the above code from another topic. (alert(uploadFormData); -> gives me a Formdata aboject)

PHP:

      move_uploaded_file($_FILES["file"]["tmp_name"],
      "ftp/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];

Can't make it work :<

The message i get from the callback function in my JS is:

Undefined index: file

  • 写回答

1条回答 默认 最新

  • douao7937 2013-08-27 21:22
    关注

    Your PHP code needs to iterate over all of the files in the request. Based on your javascript, your PHP code should look something like this:

    foreach ($_FILES["files"] as $file) {
        move_uploaded_file($file['tmp_name'], $target);
    }
    

    The $target variable must point at the local destination for your file. See the PHP manual for more details.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?