我正在尝试使用FileReaderFileReader.readAsDataURL()
API 上传图像,并在服务器端使用 PHP将方法中的数据 URL 写入文件。
该方法适用于较小的图像(不确定到底有多小,但 < 1MB 可能相当准确)。但是,在上传较大的图像时,我收到 413 Request Entity Too Large 错误。我看到了这个问题并尝试了两种解决方案,但是第一个什么也没做,第二个给出了 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();
我怎样才能使这项工作?