2011-10-12 69 views
1

好吧,在这里有点厚 - 我认为,我正在看pangratz/dnd-file-upload和拖动&下降是伟大的等(我不知道跨浏览器等,但无所谓),但什么我不能做的是创建正确的PHP代码来处理实际的上传。HTML 5文件拖放文件上传通过php

这是基础的js代码

$(document).ready(function(){ 

      $.fn.dropzone.uploadStarted = function(fileIndex, file){ 
       var infoDiv = $("<div></div>"); 
       infoDiv.attr("id", "dropzone-info" + fileIndex); 
       infoDiv.html("upload started: " + file.fileName); 

       var progressDiv = $("<div></div>"); 
       progressDiv.css({ 
        'background-color': 'orange', 
        'height': '20px', 
        'width': '0%' 
       }); 
       progressDiv.attr("id", "dropzone-speed" + fileIndex); 

       var fileDiv = $("<div></div>"); 
       fileDiv.addClass("dropzone-info"); 
       fileDiv.css({ 
        'border' : 'thin solid black', 
        'margin' : '5px' 
       }); 
       fileDiv.append(infoDiv);     
       fileDiv.append(progressDiv);     

       $("#dropzone-info").after(fileDiv); 
      }; 
      $.fn.dropzone.uploadFinished = function(fileIndex, file, duration){ 
       $("#dropzone-info" + fileIndex).html("upload finished: " + file.fileName + " ("+getReadableFileSizeString(file.fileSize)+") in " + (getReadableDurationString(duration))); 
       $("#dropzone-speed" + fileIndex).css({ 
        'width': '100%', 
        'background-color': 'green' 
       }); 
      }; 
      $.fn.dropzone.fileUploadProgressUpdated = function(fileIndex, file, newProgress){ 
       $("#dropzone-speed" + fileIndex).css("width", newProgress + "%"); 
      }; 
      $.fn.dropzone.fileUploadSpeedUpdated = function(fileIndex, file, KBperSecond){ 
       var dive = $("#dropzone-speed" + fileIndex); 

       dive.html(getReadableSpeedString(KBperSecond)); 
      }; 
      $.fn.dropzone.newFilesDropped = function(){ 
       $(".dropzone-info").remove(); 
      }; 
      $("#dropzone").dropzone({ 
       url : "upload.php", 
       printLogs : true, 
       uploadRateRefreshTime : 500, 
       numConcurrentUploads : 2 
      }); 

     }); 

但我不能让upload.php的以任何方式

$tempFile = $_FILES['Filedata']['tmp_name']; 
    $targetPath = ''; 
    $targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name']; 
move_uploaded_file($tempFile,$targetFile); 
     echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile); 

建议等欢迎我需要做的工作是上传图像化,以服务器和理智的数据库(保存不坏,一次我可以得到拖动&下降的图像移动到服务器哦,我不想使用第三方应用程序

谢谢提前

+0

我没有得到任何错误'因为upload.php脚本不起作用;) –

+0

Aggg我想我意外地从Damien删除了一条评论 - 对不起 –

+0

任何错误信息?检查了你的js console/php错误日志?如果你需要帮助,“不工作”并不是很有用。 –

回答

0

我还没有找到如何使用拖动库拖动&。因为我认为ajax请求不要设置$ _FILES。 我知道这是不是一个完整的解决方案,但如果你真的需要一个快速的答案,那就是:

jquery-filedrop

有了这个库,你可以发送POST变量设置“PARAMNAME”。

$('#dropzone').filedrop({ 
    url: 'upload.php',    // upload handler, handles each file separately 
    paramname: 'Filedata', 

与此,您的PHP代码应该工作。我已经使用过它,非常简单。 希望它有帮助。