Error handling with PHP image upload library

Asked

Viewed 29 times

-2

I searched a PHP image upload library to use in my application, I chose this one. https://dhavalkapil.com/image-uploader/

I implemented and it is working and the success of sending an image results in Boolean(true), the same as "Successfully sent image" in human terms. If there is an error what is returned is an "Exception Object" with all information related to the image, path, arguments and exceptions.

What I don’t know is to use this library return to treat the error in a user-friendly way. I know how to extract keys, array values, and to be friendly, I printed the error return inside <pre>.

The upload code is as follows::

<?php
require ("../src/ImageUploader.php");

try
{
  $imageUploader = new ImageUploader( dirname( __FILE__ ) . "/upload", "meu_salt_randomico");

  $res = $imageUploader->upload($_FILES["my_image"], "meu_id");

  var_dump($res);
}
catch (Exception $e)
{
  var_dump($e);
}
?>

And the mistake I need to fix is this::

Exception Object
(
    [message:protected] => Size limit exceeded
    [string:Exception:private] => 
    [code:protected] => 0
    [file:protected] => D:\xxxxxxxxxxxxxxx\ImageUploader.php
    [line:protected] => 202
    [trace:Exception:private] => Array
        (
            [0] => Array
                (
                    [file] => D:\xxxxxxxxxxxxxxx\ImageUploader.php
                    [line] => 239
                    [function] => checkFileSize
                    [class] => ImageUploader
                    [type] => ->
                    [args] => Array
                        (
                            [0] => Array
                                (
                                    [name] => wallpapersden.com_k-nioh-2_3936x2215.jpg
                                    [type] => image/jpeg
                                    [tmp_name] => D:\xxxxxxxxxxxxxxx\tmp\php9DDD.tmp
                                    [error] => 0
                                    [size] => 1824428
                                )

                        )

                )

            [1] => Array
                (
                    [file] => D:\xxxxxxxxxxxxxxx\ImageUploader.php
                    [line] => 316
                    [function] => securityChecks
                    [class] => ImageUploader
                    [type] => ->
                    [args] => Array
                        (
                            [0] => Array
                                (
                                    [name] => wallpapersden.com_k-nioh-2_3936x2215.jpg
                                    [type] => image/jpeg
                                    [tmp_name] => D:\xxxxxxxxxxxxxxx\php9DDD.tmp
                                    [error] => 0
                                    [size] => 1824428
                                )

                        )

                )

            [2] => Array
                (
                    [file] => D:\xxxxxxxxxxxxxxx\user-profile-photo-upload.php
                    [line] => 30
                    [function] => upload
                    [class] => ImageUploader
                    [type] => ->
                    [args] => Array
                        (
                            [0] => Array
                                (
                                    [name] => wallpapersden.com_k-nioh-2_3936x2215.jpg
                                    [type] => image/jpeg
                                    [tmp_name] => D:\xxxxxxxxxxxxxxx\php9DDD.tmp
                                    [error] => 0
                                    [size] => 1824428
                                )

                            [1] => meu_id123123123123
                        )

                )

        )

    [previous:Exception:private] => 
)

I tried to extract from the error the received values as if it were extendable by extracting values from an array, but it gives error, and I don’t know how to proceed with it. Any reference for study or guidance please?

1 answer

1


To access the exception details use the methods exposed:

public __construct ([ string $message = "" [, int $code = 0 [, Throwable $previous = NULL ]]] )
final public getMessage ( void ) : string
final public getPrevious ( void ) : Throwable
final public getCode ( void ) : mixed
final public getFile ( void ) : string
final public getLine ( void ) : int
final public getTrace ( void ) : array
final public getTraceAsString ( void ) : string
public __toString ( void ) : string
final private __clone ( void ) : void

Note that these are the only methods exposed and that no properties are exposed in the class.

I imagine you’re interested in the methods getMessage and getCode to do something like

try {
    ...
} catch(Exception $e) {
    $errorMessage = $e->getMessage();
    $errorCode = $e->getCode();
}
  • Returned the error message and could handle the information, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.