簡體   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