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.
create-resource.php
: https://pastebin.com/RaVRudTjcreate-resource-functions.php
: https://pastebin.com/NrtPGq2N
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
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.
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:
I’d write this: