【问题标题】:try catch block is not catching exception in codetry catch 块没有在代码中捕获异常
【发布时间】:2019-11-11 04:20:51
【问题描述】:

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

但我注意到,如果我用 .jpg 文件扩展名重命名文件,当我点击 exif_read_data ERROR: exif_read_data(5d17a444d54694.60141039.jpg): File not supported 时,它将被接受并抛出错误,这是完美的很好,因为我无意中希望这种情况发生以阻止上传非图像文件类型。

因此,我决定在此设置一个 try catch 块,以便在此处发生错误时抛出错误,然后我可以删除已上传的文件。

但是 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';
    }

【问题讨论】:

    标签: php exception try-catch exif


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-12
      • 2016-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多