doulangyu9636 2017-04-22 15:41
浏览 18

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

I am uploading an image. The max file size is 2M. I am checking what happens when a user tries to upload an image which is over the max. I need to trap the error and display a friendly message.

When the error occurs my program does not move the image to image directory and it should not call a function that calculates it's ratio in proportion to he screen.

No matter what I do to trap the error it does not move the file however it proceeds to calculate the ratio.

The image is not available. There is an image not found warning is displayed on screen. Secondly the getimagesize generates error , this is also displayed on screen. Thirdly it is trying to divide by 0. This error is also displayed on screen.

Although the conditions are not met the function still runs and I cannot stop the error messages from being displayed on screen. It be very unprofessional if it happens in production. Please help me stop the warnings or errors from being displayed in this manner. I was hoping the try catch would resolve. I also added user error handling. It did not stop.

code:

            //=========================================================================
            //  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;
                    }
                }
            }
  • 写回答

1条回答 默认 最新

  • dpj83664 2017-04-22 15:47
    关注

    You cant catch php errors with try-catch. You only can catch exceptions thrown with throw. To catch php errors and warnings, you have to set your own error handler, which could throw exception, which could be catched with a try catch.

    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);
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)