0

I'm trying to create a form where a user can insert multiple images. How ever, when the file input is empty, the class function (addImgToNieuws) will still run.

Here's the code:

if($_POST && !empty($_POST['title']) && !empty($_POST['description']) ) {
    $response = $mysql->addNieuwsItem(
        $_POST['title'], 
        $_POST['description'],
        $id
    );

    if(!empty($_FILES['images']) && $_FILES['images']['error'] != 4){
        $response = $mysql->addImgToNieuws(
            $_FILES['images']
        );
    }
}

The form:

<form action='' method='post' enctype='multipart/form-data' />
    <input type='text' name='title' placeholder='Titel' />
    <textarea name='description' placeholder='Descriptie'></textarea>
    <input type='file' name='images[]' multiple />
    <input type='submit' name='submit' value='Plaatsen' />
</form>

The class function:

function addImgToNieuws($images){
    echo 'Function runs';
}

EDIT: Could it be that it has something to do with the fact that it is posted as an array?

10
  • What ORM are you using here, or have you made your own somehow? addNieuwsItem is not a standard PHP function. Commented Mar 20, 2015 at 15:16
  • 2
    Why are you checking for an error code of 4? You should only proceed with your upload if the value is 0. Commented Mar 20, 2015 at 15:20
  • 1
    Try if(!empty($_FILES['images']) && $_FILES['images']['error'] === UPLOAD_ERR_OK) Commented Mar 20, 2015 at 15:23
  • 2
    Thank @jeroen - I merely used the correct PHP constant rather than (int) 0 ;) Commented Mar 20, 2015 at 15:26
  • 1
    lol - we've missed the obvious I think ... $_FILES['images'] is gonna be an array since you're doing multiple uploads... you're going to need to loop it foreach($_FILES['images']['error'] as $iError) and then check each $iError === UPLOAD_ERR_OK ;) Commented Mar 20, 2015 at 15:35

3 Answers 3

1

Since you're doing multiple file uploads $_FILES['images'] is going to be an array and you'll need to handle each image upload and error trap accordingly.

However it looks as though your addImgToNieuws() method handles the entire $_FILES['images'] array in one go so rather than calling it multiple times it might be better to just log (or capture/output) any failures.

if(!empty($_FILES['images'])) {

    $aErrors = array();
    foreach($_FILES['images'] as $aThisImage) {

        // capture any errors
        // I've put the current $_FILES['images'] array into the errors
        // array so you can check the ['name'], ['tmp_name'] or ['error']
        // for each individually
        if($aThisImage['error'] !== UPLOAD_ERR_OK) { 
            $aErrors[] = $aThisImage;
        }
    }

    //check the errors
    if($aErrors) {
        // take appropriate action for your app knowing that
        // there has been a problem with *some* images
    }

    //no errors
    else {
        $response = $mysql->addImgToNieuws(
            $_FILES['images']
        );
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect, however I think the foreach loop should look like this: foreach($_FILES['images']['error'] as $aThisImage) { if($aThisImage !== UPLOAD_ERR_OK) { $aErrors[] = $aThisImage; } }
That would only give you the error code for each image upload that failed in the $aErrors array - you'd not be able to determine which image upload actually failed - but if you're OK with that then yeah.
0

try this

 if(!empty($_FILES['images']) && $_FILES['images']['error'] != 4 && $_FILES['images'] != ''){

2 Comments

you can also use the "is_uploaded_file" check of php php.net/manual/en/function.is-uploaded-file.php
i found this on an other question : "If no file is selected for upload in your form, PHP will return $_FILES['userfile']['size'] as 0, and $_FILES['userfile']['tmp_name'] as none." Source : stackoverflow.com/questions/12538718/… Answer Jimp
0

you can try like this way:

if(isset($_POST['submit']) && isset($_POST['title']) && isset($_POST['description']) ) {
    $response = $mysql->addNieuwsItem(
        $_POST['title'], 
        $_POST['description'],
        $id
    );

    if(is_uploaded_file($_FILES['images']['tmp_name'])){
        $response = $mysql->addImgToNieuws(
            $_FILES['images']
        );
    }
}

1 Comment

Warning: is_uploaded_file() expects parameter 1 to be string, array given in /Applications/MAMP/htdocs/hb/nieuws.php on line 17, so I guess we found the problem... it's an array..

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.