Best way to validate & show errors simultaneously


#1

I have a PHP Upload class that works wonderfully on it’s own, but currently I am in need of adding more within the same controller function which is where my problem is arising. Right now I have the upload class called and made them $upload/1/2, the issue is now when there is an error with more than one of the files, it only shows one error message and not the rest so I was wondering how i could make it so they all display if all have errors and not just show one upload error over the rest so basically a error handler. Thanks very much in advance.

Here is my upload code:

                    if($data['formData']['icon_img'] != '') {
                        $upload = new Uploader($_FILES["icon_img"]);
                        $upload->allowed_extensions(array('png', 'jpg', 'jpeg', 'gif'));
                        $upload->allowed_types(array('image/jpeg','image/png','image/gif'));
                        $upload->max_size(2); // in MB
                        $upload->max_image_dimensions(array('200', '200')); // in pixels
                        $upload->path("uploads/items/thumbnail");
                        $upload->name($files['iname']);
                        if (! $upload->upload()) {
                            echo 'Upload error: ' . $upload->get_error();
                            //die();
    				    }
                    }
                    if($data['formData']['preview_img'] != '') {
                        $upload1 = new Uploader($_FILES["preview_img"]);
                        $upload1->allowed_extensions(array('png', 'jpg', 'jpeg', 'gif'));
                        $upload1->allowed_types(array('image/jpeg','image/png','image/gif'));
                        $upload1->max_size(4); // in MB
                        $upload1->max_image_dimensions(array('1600', '800')); // in pixels
                        $upload1->path("uploads/items/preview");
                        $upload1->name($files['pname']);
                        if (! $upload1->upload()) {
                            echo 'Upload error: ' . $upload1->get_error();
                            //die();
    				    }
                    }
                    if($data['formData']['item_file'] != '') {
                        $upload2 = new Uploader($_FILES["item_file"]);
                        $upload2->allowed_extensions(array('zip'));
                        $upload2->allowed_types(array('application/zip', 'application/octet-stream', 'application/x-zip-compressed', 'multipart/x-zip'));
                        $upload2->max_size(10); // in MB
                        $upload2->path("uploads/items/files/" . SECURE_FOLDER);
                        $upload2->name($files['fname']);
                        if (! $upload2->upload()) {
                            echo 'Upload error: ' . $upload2->get_error();
                            //die();
    				    }
                    }

Update: This made all the errors show but also shows other error messages.

Upload error: Invalid image dimentions.Upload error: Invalid image dimentions.
**Notice** : Undefined variable: upload2 in **C:\xampp\htdocs\new\app\controllers\Marketplace.php** on line **452**

**Fatal error** : Uncaught Error: Call to a member function get_error() on null in C:\xampp\htdocs\new\app\controllers\Marketplace.php:452 Stack trace: #0 C:\xampp\htdocs\new\app\core\App.php(92): Marketplace->itemEdit('1', '') #1 C:\xampp\htdocs\new\public\index.php(143): App->__construct(Array, Array) #2 {main} thrown in **C:\xampp\htdocs\new\app\controllers\Marketplace.php** on line **452**
                    if($data['formData']['icon_img'] != '') {
                        $upload = new Uploader($_FILES["icon_img"]);
                        $upload->allowed_extensions(array('png', 'jpg', 'jpeg', 'gif'));
                        $upload->allowed_types(array('image/jpeg','image/png','image/gif'));
                        $upload->max_size(2); // in MB
                        $upload->max_image_dimensions(array('200', '200')); // in pixels
                        $upload->path("uploads/items/thumbnail");
                        $upload->name($files['iname']);
                        if (! $upload->upload()) {
                            echo 'Upload error: ' . $upload->get_error();
    				    }
                    }
                    if($data['formData']['preview_img'] != '') {
                        $upload1 = new Uploader($_FILES["preview_img"]);
                        $upload1->allowed_extensions(array('png', 'jpg', 'jpeg', 'gif'));
                        $upload1->allowed_types(array('image/jpeg','image/png','image/gif'));
                        $upload1->max_size(4); // in MB
                        $upload1->max_image_dimensions(array('1600', '800')); // in pixels
                        $upload1->path("uploads/items/preview");
                        $upload1->name($files['pname']);
                        if (! $upload1->upload()) {
                            echo 'Upload error: ' . $upload1->get_error();
    				    }
                    }
                    if($data['formData']['item_file'] != '') {
                        $upload2 = new Uploader($_FILES["item_file"]);
                        $upload2->allowed_extensions(array('zip'));
                        $upload2->allowed_types(array('application/zip', 'application/octet-stream', 'application/x-zip-compressed', 'multipart/x-zip'));
                        $upload2->max_size(10); // in MB
                        $upload2->path("uploads/items/files/" . SECURE_FOLDER);
                        $upload2->name($files['fname']);
                        if (! $upload2->upload()) {
                            echo 'Upload error: ' . $upload2->get_error();
    				    }
                    }
                    
					if(!empty($upload->get_error() && $upload1->get_error() && $upload2->get_error())) {
                        echo $upload->get_error();
                        echo $upload1->get_error();
                        echo $upload2->get_error();
                    } else {   
                        if($this->m->updateItemApprove($formData)){
                            //$this->m->updateItemUpdateStatus($formData);
                            flash('message', 'Your changes have been submitted and will be approved within 1-2 business days!', 'alert alert-success mb-0');
                            redirect('item/edit/'.$id.'/');
                        } else{
                            die(LANG['something-went-wrong']);
                        } 
                    }

#2

< off topic>
Reminds me of “There’s a hole in my bucket dear Henry, dear Henry”…
< /off topic>

Looping with database?

Take a look at the above topic and search the online PHP Manual for the declarations and function calls for further details. Note especially which are file wide and which also apply to all included files.


#3

This is for a school project so I cannot just turn off the errors.Those errors are susposed to be there because the code is not correct. I don’t want to remove errors when they do not have to be there which is why I am after a solution to to a error bucket that is able to show all of them and not just one.

For example this here works great no problems at all because there is only one error echo but as I have mulitple inputs I need an error bucket but I have never done one before so was wondering how I would go about starting.

                if(empty($data['formData']['avatar_err'])) {
                    $upload = new Uploader($_FILES["avatar"]);
                    $upload->allowed_extensions(array('png', 'jpg', 'jpeg', 'gif'));
                    $upload->allowed_types(array('image/jpeg','image/png','image/gif'));
                    $upload->max_size(2); // in MB
                    $upload->max_image_dimensions(array('200', '200')); // in pixels
                    $upload->path("uploads/users");
                    $upload->name($files['name']);
                    //$deleteImage =  getcwd() . 'BLXUeCJvvshEYxrvHutSHNCTd.png';

                    if (! $upload->upload()) {
                        echo 'Upload error: ' . $upload->get_error();
    				} else {
                        if($this->auth->updateUserPicture($formData)) {
                            flash('message', LANG['settings-successi'], 'alert alert-success mb-0');
                            redirect('user/settings/#tab3');
        				} else {
                            die(LANG['something-went-wrong']);
        				}
    				}
                }

Thanks


#4

I just glanced at this page but think it could solve your problem.

http://php.net/manual/en/function.set-error-handler.php


#5

You need to put the errors in an array, then check if the the array has anything in it and loop over the array if there is.

$error[] = null;
$error[] = 'Some Error';
$error[] = 'Another Error';

if ($error)
{
  foreach error
  {
  // Display Error
  }
}

#6

Thankyou, thankyou! 10/10 :slight_smile:


#7

@benanamen

                    if($data['formData']['icon_img'] != '') {
                        $upload = new Uploader($_FILES["icon_img"]);
                        $upload->allowed_extensions(array('png', 'jpg', 'jpeg', 'gif'));
                        $upload->allowed_types(array('image/jpeg','image/png','image/gif'));
                        $upload->max_size(2); // in MB
                        $upload->max_image_dimensions(array('200', '200')); // in pixels
                        $upload->path("uploads/items/thumbnail");
                        $upload->name($files['iname']);
                        if (! $upload->upload()) {
                            $error[] = 'Thumbnail Image Upload: ' . $upload->get_error();
                            //echo 'Upload error: ' . $upload->get_error();
    				    }
                    }
                    if($data['formData']['preview_img'] != '') {
                        $upload1 = new Uploader($_FILES["preview_img"]);
                        $upload1->allowed_extensions(array('png', 'jpg', 'jpeg', 'gif'));
                        $upload1->allowed_types(array('image/jpeg','image/png','image/gif'));
                        $upload1->max_size(4); // in MB
                        $upload1->max_image_dimensions(array('1600', '800')); // in pixels
                        $upload1->path("uploads/items/preview");
                        $upload1->name($files['pname']);
                        if (! $upload1->upload()) {
                            $error[] = 'Preview Image Upload: ' . $upload1->get_error();
                            //echo 'Upload error: ' . $upload1->get_error();
    				    }
                    }
                    if($data['formData']['item_file'] != '') {
                        $upload2 = new Uploader($_FILES["item_file"]);
                        $upload2->allowed_extensions(array('zip'));
                        $upload2->allowed_types(array('application/zip', 'application/octet-stream', 'application/x-zip-compressed', 'multipart/x-zip'));
                        $upload2->max_size(10); // in MB
                        $upload2->path("uploads/items/files/" . SECURE_FOLDER);
                        $upload2->name($files['fname']);
                        if (! $upload2->upload()) {
                            $error[] = 'File Upload: ' . $upload2->get_error();
                            //echo 'Upload error: ' . $upload2->get_error();
    				    }
                    }
                    
                    $error[] = '';
					if(!empty($error)) {
                        foreach ($error as $err)
                        {
                            // Display Error
                            echo $err.'<br>';
                            echo 'test';
                        }
                    } else {   
                        if($this->m->updateItemApprove($formData)) {
                            //$this->m->updateItemUpdateStatus($formData);
                            flash('message', 'Your changes have been submitted and will be approved within 1-2 business days!', 'alert alert-success mb-0');
                            redirect('item/edit/'.$id.'/');
                        } else{
                            die(LANG['something-went-wrong']);
                        } 
                    }

Please can you point me in the right direction with what I am doing wrong validation wise as I can’t get it to go to the else statement. Thanks in advance.


#8

Because you have this code:

$error[] = '';
  if(!empty($error)) {

the first line ensures that $error is never empty, there’s always at least one element in it. Unless you specifically need it, try removing that first line in the snippet above.


#9

Yep, that worked thanks very much!