我正在为wordpress实现我自己的插件,该插件有一个字段可以在服务器上上传小文件。
我的作品就像:
HTML
<div id="myFormDiv" class="my_editor">
<form id="myForm" name="myForm" enctype="multipart/form-data" action="" method="POST">
<fieldset>
<label for="f_name">Name:</label>
<input type="text" name="f_name" id="f_name" class="text ui-widget-content ui-corner-all f_name">
<label for="f_icon">Icon:</label>
<input type="file" name="f_icon" id="f_icon" class="text ui-widget-content ui-corner-all f_icon">
<label for="f_active">Active:</label>
<input type="checkbox" name="f_active" id="f_active" class="text ui-widget-content ui-corner-all f_active">
<input type="submit" tabindex="-1" style="position:absolute; top:-1000px">
</fieldset>
</form>
</div>
的JavaScript
dialogForFile = $( "#myForm" ).dialog({
autoOpen: false,
height: 480,
width: 600,
modal: true,
buttons: {
"Save": function(e) {
e.preventDefault();
var itemf = {};
var jsonData = [];
....
jsonData.push(itemf);
var dataElements = {
f_name: $("#f_name").val(),
f_icon: $("#f_icon").val().substr($("#f_icon").val().lastIndexOf("\\")+1),
f_active: ($('#f_active').prop('checked') ? "1" : "0"),
file_icon: jQuery('#f_icon')[0].files[0],
};
var data = new FormData();
for (var key in dataElements) {
data.append(key, dataElements[key]);
console.log(key, dataElements[key]);
}
$.ajax({
url:"http://"+window.location.host+"/wp-content/plugins/myplugin/myplugin.db.php",
type: 'POST',
data: data,
processData: false,
contentType: false,
enctype: 'multipart/form-data',
cache: false
}).done(function (result) {
showListingMessage('Saved!');
});
dialogForFile.dialog( "close" );
//saveData("placements");
},
"Cancel": function() {
dialogForFile.dialog( "close" );
showListingMessage('Not saved!');
}
},
close: function() {
//allFields.removeClass( "ui-state-error" );
}
});
PHP myplugin.db.php
$uploadOk = 1;
$target_file_icon = UPLOAD_DIR . basename($_FILES["file_icon"]["name"]);
$imageIconFileType = pathinfo($target_file_icon,PATHINFO_EXTENSION);
switch ($_FILES['file_icon']['error']) {
case UPLOAD_ERR_OK:
break;
case UPLOAD_ERR_NO_FILE:
throw new RuntimeException('No file sent.');
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
throw new RuntimeException('Exceeded filesize limit.');
default:
throw new RuntimeException('Unknown errors.');
}
if($imageIconFileType != "ico" && $imageIconFileType != "png" ) {
echo "Sorry, only ICO & PNG files are allowed.";
$uploadOk = 0;
}
move_uploaded_file($_FILES["file_icon"]["tmp_name"], "./uploads/" . $_FILES["file_icon"]["name"]);
并且问题是我提交的时候我在$_FILES["file_icon"]["tmp_name"]
/tmp/php3iu23i2
中看到哪些应该保存在php的临时文件中,但看起来临时文件没有保存到/tmp
所以它无法移动到目标文件夹。
我查了php.ini
,没关系。没有错误......只是看起来它并没有真正地将文件从源写入/tmp
O.S。是Centos 5.8 Final和php版本是5.3.3
也许我会忘记一些事情?任何技巧将不胜感激
谢谢欢呼! 路易