Upload images and attachments from frontend form

I mentioned in a comment how it’s important to debug your code. Here’s why:

The images are added first.
In the image adding section, you’re running this line of code:

$_FILES = array("moreimages" => $image);

Then when you get to your routine that adds the files, you start with this:

$files = $_FILES['morefiles'];

Can you see what’s wrong here? At this point, $_FILES only contains "moreimages" and nothing else, because you overwrote it earlier.

You could simply create a new variable rather than resetting $_FILES (eg. $my_processed_images = array("moreimages" => $image); and then foreach ($my_processed_images...), but there’s a lot of other things that can be done to make this code more redundant and easier to follow too.

A quick point on debugging: print_r() is your friend. For example, if you’re expecting a variable to be holding something, print_r($_FILES) so you can see if it really is. This will help avoid hours of head scratching 🙂

Leave a Comment