所以我一直在開發一個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相關的東西。但我不這麼認爲。
我仍然在學習,所以任何幫助改善代碼表示讚賞!
將這個'的print_r($ _ FILES),你得到什麼? – AliN11
@ AliN11,不幸的是一個空陣列。事情是,真的很難判斷它是否有效,因爲我在表單頁面上看不到任何錯誤或回聲。 – Sj03rs
'jQuery('form')[0]'是否返回您的文件輸入的實際表單?如果在實際需要的頁面中還有其他頁面元素,可能會得到其中的一個。您是否檢查過請求標頭以確保您發送的是文件,即在您的瀏覽器開發人員工具的「網絡」選項卡中查找對端點的請求,並查看有效負載以查看它是否列出文件。 –