2013-10-23 93 views
0

我正嘗試使用AJAX上傳文件。 如果我做了vardump:使用AJAX和PHP上傳

var_dump($_FILES); 

我得到充滿信息的數組。 但是,如果我這樣做:

$try = move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]); 

$嘗試是假的誰可以幫我?

的javascript:

var formData = new FormData($('form')[0]); 
    $.ajax({ 
     url: 'process.php?command=upload', //Server script to process data 
     type: 'POST', 
     xhr: function() { // Custom XMLHttpRequest 
      var myXhr = $.ajaxSettings.xhr(); 
      if(myXhr.upload){ // Check if upload property exists 
       myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // For handling the progress of the upload 
      } 
      return myXhr; 
     }, 
     //Ajax events 
     // beforeSend: beforeSendHandler, 
     //success: completeHandler, 
     //error: errorHandler, 
     // Form data 
     data: formData, 
     //Options to tell jQuery not to process data or worry about content-type. 
     cache: false, 
     contentType: false, 
     processData: false 
    }); 
}); 

function progressHandlingFunction(e){ 
    if(e.lengthComputable){ 
     $('progress').attr({value:e.loaded,max:e.total}); 
    } 
} 

後續代碼var_dump從$ _FILES

array(1) { 
    ["file"]=> 
    array(5) { 
    ["name"]=> 
    string(5) "1.jpg" 
    ["type"]=> 
    string(10) "image/jpeg" 
    ["tmp_name"]=> 
    string(14) "/tmp/phpATNNIs" 
    ["error"]=> 
    int(0) 
    ["size"]=> 
    int(220559) 
    } 
} 

感謝

+0

和JavaScript代碼在哪裏? –

+0

看這裏http://net.tutsplus.com/tutorials/javascript-ajax/uploading-files-with-ajax/ –

+0

你能否給我們$ _FILES的整個輸出? –

回答