我有以下一段代码,可以上传图像并根据方向旋转图像,还可以进行一些基本的文件类型检查。
但是我注意到,如果重命名带有.jpg文件扩展名的文件,当我按下exif_read_data
时,它将被接受并引发错误:exif_read_data(5d17a444d54694.60141039.jpg):不支持该文件非常好,因为我无意间希望这种情况能够阻止非图像文件类型的上传。
因此,我决定在此附近尝试尝试捕获块以引发错误(如果此处发生错误),那么我可以删除上传的文件。
但是try catch块没有捕获错误,我不明白为什么。
有什么主意,我该如何获得所需的东西?
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';
}
答案 0 :(得分:0)
我必须设置set_error_handler()
和ErrorException类,才能将所有php错误转化为异常。发现另一个问题
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);
});