0

I have a html form like so:

<form method="post" enctype="multipart/form-data">
    <input type="file" name="pictures[]" required>
    <input type="file" name="pictures[]">
    <input type="file" name="pictures[]">
    <input type="file" name="pictures[]">
    <input type="file" name="pictures[]">
</form>

such that, a user can upload up to 5 pictures. I know that I can do it with <input type="file" name="pictures[]" multiple>, but the idea is that, I want the users to be able to select files from different folders of their device(s), hence the reason why I wrote in the format above.

Now, the challenge I am having now is that, not all the inputs are required, and when the form is submitted, and I do the normal input checks $_FILES['pictures']['name], it loops through all the file array, which is normal since they have the same name.

Now my headache..

  • How do I validate only the inputs that a file is entered while maintaining the one required field?

For now, if I do a check $_FILES['pictures']['name'] when less than 5 files are added in the input boxes, it returns an error because of the input field(s) that were not entered.

EDIT

First I check if at least one file was entered, then I check if any of the files do not return any error. See my code below:

if ( count($_FILES['pictures']['name']) < 1 ) {
    echo "Please enter at least one image";
}
elseif ( $_FILES['pictures']['error'] != 0 ) {
    echo "Sorry, one or more of your uploaded images is invalid";
}

Now the form will always return an error if not all the 5 inputs fields are entered.

ANOTHER EDIT

Maybe I wasn't able to explain clearly what I wanted to achieve.. but here's the gist.

Apart from the fact that one of the inputs is required, when the form is submitted without all the fields entered, and I do something like so (skipping the required check):

$files = array();
$fileData = $_FILES['pictures'];
$files = array();
if ( is_array($fileData['name']) ) {

    for ( $i=0; $i < count($fileData['name']) ; ++$i ) {
        $files[] = array(
            'name' => $fileData['name'][$i],
            'tmp_name' => $fileData['tmp_name'][$i],
            'type' => $fileData['type'][$i],
            'error' => $fileData['error'][$i],
            'size' => $fileData['size'][$i],
        );        
    }
}
else {
    $files[] = $fileData;
}

So when I perform validations like:

foreach ($files as $file) {
    if ( $file['name'] == '' ) {
        echo "Error";
    }
}

, and the user did not enter all the images, it returns the error message.

A print_r() of the $files returns this:

Array
(
    [photos] => Array
        (
            [name] => Array
                (
                    [0] => 11356010_127900540886840_1152019271_n.jpg
                    [1] => 11370980_130679033939470_1067474802_n.jpg
                    [2] => 
                    [3] => 
                    [4] => 
                )

            [type] => Array
                (
                    [0] => image/jpeg
                    [1] => image/jpeg
                    [2] => 
                    [3] =>
                    [4] =>  
                )

            [tmp_name] => Array
                (
                    [0] => C:\xampp7\tmp\php1C9F.tmp
                    [1] => C:\xampp7\tmp\php1CaF.tmp
                    [2] => 
                    [3] => 
                    [4] => 
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 4
                    [3] => 4
                    [4] => 4
                )

            [size] => Array
                (
                    [0] => 108492
                    [1] => 108492
                    [2] => 0
                    [3] => 0
                    [4] => 0
                )

        )

    [files] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
        )

)

Is there a way I can make the empty fields to be ignore when doing my checks?

I know I can do this with javascript by checking for the empty fields and add the attribute of disabled on submit, but what if the user turned off javascript in his/her browser, what happens?

I've tried using array_filter() like so:

$filtered = array_filter($_FILES['pictures'], function($var){
    return !empty($var['pictures']['name']);
});

, but it returns an empty array. Can someone please point me to the right direction?

8
  • If something is inputed then name key is not empty. If something is inputed and uploaded successfully then error key is 0. Commented Sep 15, 2017 at 19:07
  • Yes, but if less than 5 files are uploaded and I do a check for files with empty name, it returns an error, because of the other input that was not entered. Commented Sep 15, 2017 at 19:09
  • 1
    I don't see the code which checks, I can't help you. Commented Sep 15, 2017 at 19:13
  • 1
    Can you please show some code demonstrating what you've tried but isn't working? I think that would help demonstrate the problem. Commented Sep 15, 2017 at 19:14
  • use jQuery to validate and then $( "form").submit(); Commented Sep 15, 2017 at 19:15

2 Answers 2

1

$_FILES['pictures']['error'] will end up being an array of error codes, so you can loop over it and handle them explicitly. There is a good example of this in the PHP documentation.

In your case, you may want to handle the first iteration of the loop as a special case, since you've defined the first one as being required. Something like this:

foreach ($_FILES["pictures"]["error"] as $key => $error) {
    if ($key == 0 && $error != UPLOAD_ERR_OK) {
        // the required one didn't succeed, so we have a problem
    } else {
        // ...
Sign up to request clarification or add additional context in comments.

7 Comments

How do I check for the other inputs based on how many that were actually entered?
That would be the else part of this solution. As you continue looping, you will see if there were other errors. If not, just proceed. Have you tried var_dump($_FILES["pictures"]); to see what that whole array looks like? That might help with the understanding of it.
Yes I have done that, and it returned empty for the name and tmp_name, 0 for the size and an error because no files were seen from the remaining items in the emtpy array.
I copied your code exactly, and after uploading one file via the "required" input, my var_dump looks like this: pastebin.com/fbPCatMz What are you doing differently to get empty and zero?
Exactly! When I do a foreach loop to check if any of the uploaded files returned an error 4, it returns true, if all the input fields are not entered.
|
0

because it is an array use in_array() http://php.net/manual/en/function.in-array.php

<?php
$os = $_FILES["pictures"]["error"];
 if (in_array("0", $os)) {
echo "done";
}else{
echo "error";
}
 ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.