skip to Main Content

I’m relatively new to PHP and currently working on a project where I need to create a folder and upload files based on form submissions. I’ve set up the process in two files: create-resource.php for form handling and function execution, and create-resource-functions.php for the actual functions like folder creation and file uploads.

My goal is to conditionally create the folder and upload files only if the $errors array is empty. Currently, the folder is created regardless of any errors. (And only the .jar file is uploaded, the images aren’t and they return an error that the folder doesn’t exist?)

I’m aware that my code is all over the place and needs refinement, and I’m committed to improving it.
I appreciate any advice or examples you can provide. Thank you in advance for your help!

2

Answers


  1. Chosen as BEST ANSWER

    I solved the problem by making separate functions for handling errors and doing the necessary tasks. I tweaked the code to match the rest and work correctly. I had to move some things around, but now it works.


  2. Function arguments flagged as byref such as &$errors aren’t necessarily wrong, but they are a code smell for me because I personally only need to do this in exceptional situations. In your case, create_resource_banner($resource_id, &$errors) only uses $errors for checking if there was an error elsewhere which is weird and defeats encapsulation. The code that calls function should only do so if there are no previous errors.

    So instead of this:

    $bannerResult = create_resource_banner($resource_id, $errors);
    if ($bannerResult !== null && $bannerResult['error'] && isset($bannerResult['message'])) {
        $errors[] = $bannerResult['message'];
    }
    

    I’d write this:

    if(empty($errors)) {
        $bannerResult = create_resource_banner($resource_id);
        if ($bannerResult !== null && $bannerResult['error'] && isset($bannerResult['message'])) {
            $errors[] = $bannerResult['message'];
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search