0

I have created an html form with a php back-end that allows for up to two image uploads. Everything works just fine when a user submits two images, but the submission fails if there are one or no images attached. I know why this is happening, but need a tiny bit of help.

My php is set up to handle a variable number of images, but I am missing one important detail that is preventing it from working - I don't know how to get a count of the total number of attached images.

Here is my html:

<div class="field">
   Select first image to upload:
   <input type="file" name="fileToUpload[0]" id="fileToUpload">
</div>

<div class="field">
   Select second image to upload:
   <input type="file" name="fileToUpload[1]" id="fileToUpload">
</div>

In the beginning of my php, I was trying to get the count of images using $total_images = count($_FILES['fileToUpload']['name']);, but this always returns '2' since it is counting the fields and not the fields with attachments.

How can I get a count of the number of fields with attachments, not just the total number of fields?

Thank you very much.

skwidbreth
  • 7,888
  • 11
  • 58
  • 105
  • So count using other key - `error` for example. – u_mulder Jan 26 '16 at 21:24
  • Your `count` should be accurate. Do a `print_r` of `$_FILES` and see if it contains what you expect it to. `$_FILES['fileToUpload']['name']` should be an array of the original uploaded files' filenames. – ceejayoz Jan 26 '16 at 21:24
  • you need to loop on $_FILES and check each individual file element for `['error'] == UPLOAD_ERR_OK`. just because a particular file input has a matching $_FILES entry means NOTHING to whether the corresponding upload occured, or succeeded. a file input that never had a file added to it will STILL produce a $_FILES entry, with `UPLOAD_ERR_NO_FILE` – Marc B Jan 26 '16 at 21:25
  • I think this might help you: [http://php.net/manual/en/features.file-upload.multiple.php#53240](http://php.net/manual/en/features.file-upload.multiple.php#53240) – Tony Vlcek Jan 26 '16 at 21:26

2 Answers2

1

Just count files which were uploaded without errors:

$file_counter = 0;
foreach ($_FILES["fileToUpload"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $file_counter++;
    }
}
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • I think that this will be the correct solution, but it makes me think that I actually need to change the way I have my php set up - my fault, I didn't include enough info in my question. The problem is that I need to get the count of attachments before the files are uploaded - but I think I can work a way around this and use what you have provided here. Thank you, I'm pretty much learning php as I go, didn't think about counting errors. – skwidbreth Jan 26 '16 at 21:43
  • you are welcome. If you want to count the attachments before sending POST request with uploaded files - http://stackoverflow.com/questions/6171013/javascript-get-number-of-files-and-their-filenames-from-file-input-element-with. But that's just for specific and particular cases – RomanPerekhrest Jan 26 '16 at 22:02
  • Yes - after reworking the way my php handles image uploads and db entries, this this the solution to my problem. Again, thanks very much. – skwidbreth Jan 28 '16 at 15:32
0

count($_FILES['fileToUpload]); Should do it