繁体   English   中英

尝试catch块未捕获代码中的异常

[英]try catch block is not catching exception in code

我下面的代码可以上传图像并根据方向旋转图像,还可以进行一些基本的文件类型检查。

但是我注意到,如果重命名带有.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';
    }

我必须设置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);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM