Sorry I took so long to get back. I'm posting the modified PHP for review and reuse.
In Firefox and other browsers that support sendAsBinary:
Code:
$ext = strtolower(getExtension($_FILES['upload']['name']));
$theFileName = strtolower($_FILES['upload']['name']);
$size = filesize($_FILES['upload']['tmp_name']);
if ($size > config::max_size * 1024){
echo "Error: ".$theFileName." is too big";
die();
}
if (($ext != "jpg") && ($ext != "jpeg") && ($ext != "png") && ($ext != "gif")){
echo "Error: ".$theFileName." is not a supported file type";
die();
}
if(getimagesize($_FILES['upload']['tmp_name']) == FALSE){
if(isset($_GET['base64'])){
echo "TESTING";
}else if(0 < preg_match('/^data:image\/.+?;base64,/', file_get_contents($_FILES['upload']['tmp_name']))){
$contents = file_get_contents($_FILES['upload']['tmp_name']);
file_put_contents($_FILES['upload']['tmp_name'], base64_decode(preg_replace('/^data:image\/.+?;base64,/', '', $contents)));
$size = filesize($_FILES['upload']['tmp_name']);
}else{
echo "Error: Unknown";
die();
}
}
and for Chrome (which always sends in base64 but, I've discovered, sometimes prefixes the data with the MIME type) and any other browsers that put the image data in php://input rather than $_FILES:
Code:
if(isset($_GET['base64'])) {
$content = file_get_contents('php://input');
if(0 < preg_match('/^data:image\/.+?;base64,/', $content, $matches)){
$content = preg_replace('/^data:image\/.+?;base64,/', '', $content);
}
$content = base64_decode($content);
} else { // not sure this can ever happen...
$content = file_get_contents('php://input');
if(0 < preg_match('/^data:image\/.+?;base64,/', $content, $matches)){
$content = base64_decode(preg_replace('/^data:image\/.+?;base64,/', '', $content));
}
}
$headers = getallheaders(); // UP-* headers are sent by JavaScript
$headers = array_change_key_case($headers, CASE_UPPER); //different case was being used for different browsers
$ext = strtolower(getExtension($headers['UP-FILENAME']));
$size = $headers['UP-SIZE'];
if (($ext != "jpg") && ($ext != "jpeg") && ($ext != "png") && ($ext != "gif")){
echo "Error: ".$theFileName." is too big";
die();
}
if ($size > config::max_size * 1024){
echo "Error: ".$theFileName." is not a supported file type";
die();
}
Bookmarks