我正在尝试上传一个大于限制的文件,因为我的错误不是“Exceeded filesize limit。”,我的代码如下所示:
JS
self.on('drop', function(e) {
e.preventDefault();
var prompt;
var org = e.originalEvent;
var files = org.dataTransfer.files || org.target.files;
var token = terminal.token();
if (!token) {
return;
}
if (files.length) {
files = [].slice.call(files);
(function upload() {
var file = files.shift();
function uploadFile() {
var formData = new FormData();
formData.append('file', file);
formData.append('token', token);
formData.append('path', leash.cwd);
animation = true;
var anim = ['/', '-', '\\', '|'], i = 0, delay = 400;
var timer;
(function animation() {
terminal.set_prompt(anim[i++]);
if (i > anim.length-1) {
i = 0;
}
timer = setTimeout(animation, delay);
})();
function finish() {
clearTimeout(timer);
terminal.set_prompt(prompt);
animation = false;
}
$.ajax({
url: 'lib/upload.php',
type: 'POST',
success: function(response) {
finish();
if (response.error) {
terminal.error(response.error);
} else {
terminal.echo('File "' + file.name + '" ' +
'uploaded.');
}
upload();
},
error: function(jxhr, error, status) {
terminal.error(jxhr.statusText);
finish();
},
data: formData,
cache: false,
contentType: false,
processData: false
});
}
if (file) {
prompt = terminal.get_prompt();
var fname = leash.cwd + '/' + file.name;
leash.service.file_exists(fname)(function(err, exists) {
if (exists) {
var msg = 'File "' + file.name + '" exists do you'+
' want to overwrite (Y/N)? ';
terminal.push(function(yesno) {
if (yesno.match(/^y$/i)) {
// upload
terminal.pop();
uploadFile();
} else if (yesno.match(/^n$/i)) {
terminal.pop();
upload();
}
}, {
prompt: msg
});
} else {
uploadFile();
}
});
}
})();
}
}).on('dragover', function(e) {
e.preventDefault();
}).on('dragenter', function(e) {
e.preventDefault();
});
和 php :
require('Service.php');
header('Content-type: application/json');
$service = new Service('config.json', preg_replace("/\/[^\/]+$/", "", getcwd()));
if (isset($_POST['token']) && isset($_POST['path'])) {
if ($service->valid_token($_POST['token'])) {
if (!isset($_FILES['file'])) {
echo json_encode(array('error' => 'No File'));
exit();
}
$fname = basename($_FILES['file']['name']);
switch ($_FILES['file']['error']) {
case UPLOAD_ERR_OK:
$full_name = $_POST['path'] . '/' . $fname;
if (file_exists($full_name) && !is_writable($full_name)) {
echo json_encode(array('error' => 'File "'.$fname.'" is not writable'));
} else {
if (!move_uploaded_file($_FILES['file']['tmp_name'],
$full_name)) {
echo json_encode(array('error' => 'Can\'t save file.'));
} else {
echo json_encode(array('success' => true));
}
}
break;
case UPLOAD_ERR_NO_FILE:
echo json_encode(array('error' => 'File not sent.'));
break;
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
echo json_encode(array('error' => 'Exceeded filesize limit.'));
break;
default:
echo json_encode(array('error' => 'Unknown errors.'));
}
} else {
echo json_encode(array('error' => 'Invalid Token'));
}
} else {
echo json_encode(array('error' => 'Wrong request'));
}
当我上传文件大于限制时我得到错误“错误请求”任何人都知道为什么?如果文件较小,则文件正在正确上传。
更新:如果我回显$ _POST和$ _FILES数组,则它们为空:
echo json_encode(array(
'error' => 'Wrong request',
'POST' => $_POST,
'FILE' => $_FILES
));