I have the below piece of code that uploads an image and rotates it based on the orientation it also does some basic file type checks.
But I have noticed that if I rename a file with say .jpg file extension it will be accepted and throw an error when I hit exif_read_data
ERROR: exif_read_data(5d17a444d54694.60141039.jpg): File not supported which is perfectly fine as I inadvertently want this to happen to stop a non-image filetype being uploaded.
So, I decided to but a try catch block around this to throw an error if an error occurred here then I could delete the file that was uploaded.
but the try catch block is not catching the error and I do not understand why.
Any idea how I can get this to achieve what I need?
if(isset($_FILES['file'])){
$file = $_FILES['file'];
$file_name = $_FILES['file']['name'];
$file_tmp_name = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_error = $_FILES['file']['error'];
$file_type = $_FILES['file']['type'];
$file_ext = explode('.', $file_name);
$file_actual_ext = strtolower(end($file_ext));
$allowed = array('jpg', 'jpeg', 'png');
if (in_array($file_actual_ext, $allowed)) {
if ($file_error === 0) {
if ($file_size < 6000000) {
$site_path = '/GunStalker-Website-Forms/Examples/login real/advert_image_uploads/';
$file_name_new = uniqid('', true) . "." . $file_actual_ext;
$root = $_SERVER['DOCUMENT_ROOT'];
$file_destination = $root . $site_path . $file_name_new;
move_uploaded_file($file_tmp_name, $file_destination);
try{
if ($file_actual_ext != 'jpg' && $file_actual_ext != 'jpeg' ){
convertToJpeg($file_destination, $file_destination, $quality = 100);
}
$exif_data = exif_read_data($file_destination );
$orientation = orientation($exif_data);
$degree = orientation_rotate($orientation);
$image_data = imagecreatefromjpeg($file_destination);
$image_rotate = imagerotate($image_data,$degree,0 );
imagejpeg($image_rotate, $file_destination);
imagedestroy($image_rotate);
imagedestroy($image_data);
$primary_flag = null; //Testing
$file_destination = $site_path . $file_name_new;
include 'advert_new_gun_save_image_script.inc.php';
}
catch (\Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
include 'advert_new_dropdown_populate/advert_new_gun_image_populate.php';
//<input type="submit" name="submitimage" id="submitimage" value="'. $getadvertimages_row['image_id'] . '" text="X" class="remove-image" style="display: inline;" >
} else {
echo "Error Uploading File this is too large";
include 'advert_new_dropdown_populate/advert_new_gun_image_populate.php';
}
} else {
echo "Error Uploading File";
include 'advert_new_dropdown_populate/advert_new_gun_image_populate.php';
}
} else {
echo "You cannot do this";
include 'advert_new_dropdown_populate/advert_new_gun_image_populate.php';
}
// File upload configuration
}
else{
echo "Error Please Check File";
include 'advert_new_dropdown_populate/advert_new_gun_image_populate.php';
}
I had to set set_error_handler()
and the ErrorException class to turn all php errors into exceptions. Found on another question
set_error_handler(function($errno, $errstr, $errfile, $errline, array $errcontext) {
// error was suppressed with the @-operator
if (0 === error_reporting()) {
return false;
}
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.