0

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 Answers1

1

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);
});
DarkBee
  • 16,592
  • 6
  • 46
  • 58
Philipp
  • 15,377
  • 4
  • 35
  • 52