I'm trying to upload images using the FileReader API, and writing the data URL from the FileReader.readAsDataURL()
method to a file using PHP on the server side.
The method works with smaller images (not sure exactly how small, but < 1MB is probably fairly acurate). However, when uploading larger images, I get a 413 Request Entity Too Large error. I saw this question and tried both of the solutions, however the first did nothing and the second gave a 500 error. When I checked the error log it said:
/home/username/public_html/.htaccess: LimitRequestFieldsize not allowed here
Why isn't this working?
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();
How can I make this work?