2016-03-08 28 views
0

所以我一直在開發一個WordPress插件。
因爲我已經爲我上傳文件的應用程序,我認爲這將是容易的,但唉,我想不出我做錯了什麼。

問題是;我的$_FILES['image']未設置。

任何人都可以告訴我我的代碼有什麼問題,因爲我找不到它是什麼。

<form action="" method="POST" enctype="multipart/form-data"> 
      <table class="table ws-form-table"> 
       <tbody> 
        <tr> 
         <td> 
          Add Text: 
         </td> 
         <td> 
          <input type="text" id="ws-text"> 
         </td> 
         <td> 
          <button type="button" class="btn btn-primary ws-add-text ws-add-button">+</button> 
         </td> 
        </tr> 
        <tr> 
         <td> 
          Add Image: 
         </td> 
         <td> 
          <input type="file" name="image"><progress></progress> 
         </td> 
         <td> 
          <button type="button" class="btn btn-primary ws-add-image ws-add-button">+</button> 
         </td> 
        </tr> 
       </tbody> 
      </table> 
      <div class="preview-container"> 
       <div class="preview-strict"> 
        <img class="ws-image" src="<?php echo $feat_image; ?>" alt="" style="width: 300px; height: 300px;"> 
       </div> 
      </div> 
     </form> 

JS

jQuery('.ws-add-image').click(function() { 
    var formData = new FormData(jQuery('form')[0]); 
    console.log('Click Initiated'); 
    console.log('Ajax Try...'); 
    jQuery.ajax({ 

     url: '../../wp-content/plugins/my-plugin/my-plugin-handler.php', 
     type: 'POST', 
     xhr: function() { 
      var myXhr = jQuery.ajaxSettings.xhr(); 
      if(myXhr.upload){ 
       myXhr.upload.addEventListener('progress',progressHandlingFunction, false); 
      } 
      return myXhr; 
     }, 

     data: formData, 

     cache: false, 
     contentType: false, 
     processData: false, 
     error: function() { 
      console.log('Error Initiated'); 
     }, 
    }).done(function() { 
     alert('dsa'); 
     jQuery('.preview-strict').prepend('<div id="dragHelper" style="display:inline-block; z-index: 999; cursor: move;"><img id="theImg" src="../../wp-content/plugins/my-plugin/images/' + readCookie('image') + '" width="200px" height=200px; /></div>'); 
     jQuery("#dragHelper").draggable({drag: function(){ 
      var offset = jQuery(this).offset(); 
      var xPos = offset.left; 
      var yPos = offset.top; 
      jQuery('#posX').text('x: ' + xPos); 
      jQuery('#posY').text('y: ' + yPos); 
     } 
     }); 

     jQuery("#theImg").resizable(); 

    }); 
    alert(readCookie('image')); 
    console.log('Done!'); 
}); 
function progressHandlingFunction(e){ 
    if(e.lengthComputable){ 
     jQuery('progress').attr({value:e.loaded,max:e.total}); 
    } 
} 

PHP

<?php 
ini_set('display_errors', 1); 
ini_set('display_startup_errors', 1); 
error_reporting(E_ALL); 
include_once($_SERVER['DOCUMENT_ROOT'].'/test/wp-load.php'); 
global $wpdb; 
$table_name = $wpdb->prefix . "my-plugin"; 

if(isset($_FILES['image'])) { 
    $wty = 'ISSET'; 
     $sql = $wpdb->get_results("SELECT ID FROM " . $table_name . " ORDER BY ID DESC LIMIT 1"); 
     echo $_FILES['image']['name']; 
     foreach($wpdb->get_results("SELECT ID FROM " . $table_name . " ORDER BY ID DESC LIMIT 1") as $key => $row) { 
      $id = $row->ID; 
     } 

     $temp = explode(".", $_FILES["image"]["name"]); 
     $last = $id . round(microtime(true)) . '.' . end($temp); 
     $errors= array(); 
     $file_name = $_FILES['image']['name']; 
     $file_size = $_FILES['image']['size']; 
     $file_tmp = $_FILES['image']['tmp_name']; 
     $file_type = $_FILES['image']['type']; 
     $file_ext=strtolower(end(explode('.',$_FILES['image']['name']))); 

     $ext = end((explode(".", $file_name))); 

     $expensions= array("jpeg","jpg","png"); 

     if(empty($errors)==true) { 

     move_uploaded_file($file_tmp, ABSPATH . "test/wp-content/plugins/my-plugin/images/" . $last); 

     } 

     else 

     { 

     print_r($errors); 

     } 
} 


$cookie_name = "image"; 
$cookie_value = $wty; 
$cookie_exp = time() + (86400 * 30); 



setcookie($cookie_name, $cookie_value, $cookie_exp, "/"); 


?> 

所以這個我它是如何工作的:圖像被選中,按鈕被點擊,單擊事件運行,ajax運行一個PHP文件。 PHP文件(需要)上傳圖片,創建帶有圖片名稱的cookie。圖片名稱從cookie中獲取,並在ajax完成後將其添加到DOM。

我試圖查看這個,但由於某種原因,我找不到任何東西,但人們說我可能會忘記enctype="multipart/form-data"

那麼我做錯了什麼?

請記住,這是一個WordPress插件。所以它可能是WordPress相關的東西。但我不這麼認爲。

我仍然在學習,所以任何幫助改善代碼表示讚賞!

+0

將這個'的print_r($ _ FILES),你得到什麼? – AliN11

+0

@ AliN11,不幸的是一個空陣列。事情是,真的很難判斷它是否有效,因爲我在表單頁面上看不到任何錯誤或回聲。 – Sj03rs

+1

'jQuery('form')[0]'是否返回您的文件輸入的實際表單?如果在實際需要的頁面中還有其他頁面元素,可能會得到其中的一個。您是否檢查過請求標頭以確保您發送的是文件,即在您的瀏覽器開發人員工具的「網絡」選項卡中查找對端點的請求,並查看有效負載以查看它是否列出文件。 –

回答

0

你缺少以下,而做飯了您的AJAX請求:

formData.append('image', $('input[type=file]')[0].files[0]); 
+0

謝謝,但不幸的是,它並沒有爲我做任何事情。因爲您可以看到我發佈的代碼實際上已被複制和粘貼。此代碼來自正在工作的上傳功能。我改變的唯一事情就是路徑。但是,如果您有任何其他建議,我爲此而努力! – Sj03rs

+1

FormData對象自動添加所有的表單輸入,當構造函數傳遞一個表單DOM元素,OP已經完成:'新FormData(jQuery('form')[0]);' –

1

此代碼將單個或多個文件。 `在你的PHP file.What

$('.ws-add-image').click(function() { 

var data = new FormData(); 

//Append files infos 
jQuery.each($(this)[0].files, function(i, file) { 
    data.append('file-'+i, file); 
}); 

$.ajax({ 
    url: "my_path", 
    type: "POST", 
    data: data, 
    cache: false, 
    processData: false, 
    contentType: false, 
    context: this, 
    success: function (msg) { 
      alert(msg); 
     } 
    }); 
}); 

然後在你的PHP文件,你使用會得到文件..

$_FILES['file-0'] 
$_FILES['file-1'] 
+0

'.ws-add-image'是一個按鈕,所以'$(this)[0]'將成爲沒有'files'屬性的按鈕DOM元素。如果選擇正確的表單元素,OP也會使用新的FormData(jQuery('form')[0]);'將會自動向formdata對象添加輸入 –

+0

Sanjay非常感謝您讓我走向正確的方向!你的代碼告訴我這是我放棄的錯誤道路。總而言之,它是固定的!乾杯。 – Sj03rs