2011-10-12 75 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代碼應該工作。我已經使用過它,非常簡單。 希望它有幫助。