PHP:处理错误消息后,它们仍在屏幕上显示.未满足功能的条件,但是功能仍在运行



我正在上传图像。最大文件大小为2m。我正在检查用户尝试上传最大图像时会发生什么。我需要捕获错误并显示友好的消息。

当错误发生时,我的程序不会将图像移至图像目录,并且不应调用与HE屏幕成比例计算其比率的函数。

无论我如何捕获错误,它都不会移动文件,但是它继续计算比率。

图像不可用。屏幕上没有发现未发现警告。其次,getImagesize生成错误,这也显示在屏幕上。第三,它试图除以0。此错误也显示在屏幕上。

尽管无法满足条件,但该功能仍然运行,我无法阻止错误消息显示在屏幕上。如果在生产中发生这种情况,那将是非常不专业的。请帮助我停止以这种方式显示警告或错误。我希望尝试捕获能够解决。我还添加了用户错误处理。它没有停止。

代码:

            //=========================================================================
            //  RETRIEVE IMAGE - COMPANY
            //=========================================================================
            if (empty($_SESSION["errormessage"]))
            {
                if (isset($_FILES["image"]))
                {       
                    $errors= array();
                    $file_name = $_FILES['image']['name'];
                    $file_size = $_FILES['image']['size'];
                    $file_tmp = $_FILES['image']['tmp_name'];
                    $file_type = $_FILES['image']['type'];      
                    if ($file_name != "")
                    {
                        //echo "filename: " . $file_name . "<br>";
                        $file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
                        $expensions= array("jpg","png","bmp","gif");
                        if(in_array($file_ext,$expensions)=== false)
                        {
                            $_SESSION["errormessage"] ="Extension not allowed, choose a JPEG or PNG or Bitmap or GIF file.";
                        }
                        if($file_size > 2097152) /////2mb
                        {
                            $_SESSION["errormessage"] ='File size must be excately 2 MB';
                            //[enter image description here][1]echo $_SESSION["errormessage"];
                        }
                        if(empty($errors)==true) 
                        {
                            if (empty($_SESSION["errormessage"]))
                            {
                                move_uploaded_file($file_tmp,$path.$file_name);
                                setimageratio($path.$file_name,$size,$width,$height,$ratio);
                                //========================================================
                                //Get actual size of user  image  to  proportion  correctly
                                //width is first in array and height is second. Echo if you
                                //need other values.
                                //========================================================
                            }
                        }   
                    }   
                }
                else
                {
                    echo "File is not set<br>";
                }
            }
            //=============================================================
            function setimageratio($ImageFull,&$size,&$width,&$height,&$ratio)
            {
                //========================================================
                //Get actual size of user  image  to  proportion  correctly
                //width is first in array and height is second. Echo if you
                //need other values.
                //========================================================  
                if (empty($_SESSION["errormessage"]))
                {
                    try 
                    {
                        //echo 'here: ' . $ImageFull . '<br>';  
                        $ind = 0;
                        $size = getimagesize("$ImageFull");
                        foreach ($size as $item)
                        {
                            if ($ind == 0)
                            {
                                $width = $item;
                            }
                            if ($ind == 1)
                            {
                                $height = $item;
                            }
                            $ind++;
                        }       
                //========================================================      
                //therefore: less is in relationship to 
                //greater. Ratio 
                //========================================================
                        if ($height >= $width)
                        {
                            $ratio = $width / $height;                  
                        }
                        else
                        {
                            $ratio = $height / $width;                      
                        }
                //echo "width: " . $width . '<br>';
                //echo "height: " . $height. '<br>';
                //echo "ratio: " . $ratio. '<br>';
                //echo "ImageFull: " . $ImageFull. '<br>';
                    } 
                    catch (Exception $e) 
                    {       
                        $_SESSION["errormessage"]= "Error Set Image ratio: " . $e;
                    }
                }
            }

您无法使用try-catch捕获PHP错误。您只能捕获throw的异常。要捕获PHP错误和警告,您必须设置自己的错误处理程序,这可能会引发异常,可以通过尝试捕获。

set_error_handler(function ($severity, $msg, $file, $line, array $context)
{
    // error was suppressed
    if (0 === error_reporting()) {
        return false;
    }
    throw new ErrorException($msg, 0, $severity, $file, $line);
});

最新更新