【发布时间】:2018-06-18 22:53:05
【问题描述】:
我正在尝试使用FileReader API 上传图像,并在服务器端使用PHP 将FileReader.readAsDataURL() 方法中的数据URL 写入文件。
该方法适用于较小的图像(不确定到底有多小,但 this question 并尝试了两种解决方案,但是第一个什么也没做,第二个给出了 500 错误。当我检查错误日志时,它说:
/home/username/public_html/.htaccess: LimitRequestFieldsize not allowed here
为什么这不起作用?
php.ini
file_uploads = On
post_max_size = 50M
upload_max_filesize = 50M
.htaccess
RewriteEngine On
# LimitRequestFieldSize 2097152
SSLRenegBufferSize 1000000
JS
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$.ajax({
url: "?action=saveImg",
type: "POST",
dataType: "json",
data: {
image: e.target.result,
id: id
},
success: function(data){
$.dialog(data.message, data.title);
}
});
}
for(var i = 0; i < input.files.length; i++){
reader.readAsDataURL(input.files[i]);
}
}
}
$("#file").change(function() {
readURL(this);
});
PHP
$id = $_POST["id"];
$filetype = str_replace('data:', '', explode(";", $_POST["image"])[0]);
$data = $_POST["image"];
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
$rand = random_str(6);
$ext = explode("/", $filetype)[1];
if(!is_dir("../../images/product/{$id}/")){
mkdir("../../images/product/{$id}/");
}
$fh = fopen("../../images/product/{$id}/{$rand}.{$ext}", "w+");
fwrite($fh, $data);
fclose($fh);
$return = ["message" => "ID is $id. Saved image as /images/product/{$id}/{$rand}.{$ext}.", "title" => "Saved Image", "image" => "/images/product/{$id}/{$rand}.{$ext}"];
echo json_encode($return);
die();
我怎样才能做到这一点?
【问题讨论】:
-
您确定您正在阅读正确的错误行吗?
-
非常确定。还能是什么?
-
不知
LimitRequestFieldsize指令是否与413 Request Entity Too Large错误有关。 -
... 显然它已被记录,因为您直接在 .htaccess 文件中使用了您不应该使用的指令。
-
我看到的答案似乎认为
LimitRequestFieldsize有事可做,我还应该在哪里使用该指令?我在 Godaddy 共享服务器上。
标签: javascript php .htaccess http-status-code-413