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?
addNieuwsItemis not a standard PHP function.4? You should only proceed with your upload if the value is0.if(!empty($_FILES['images']) && $_FILES['images']['error'] === UPLOAD_ERR_OK)(int) 0;)$_FILES['images']is gonna be an array since you're doing multiple uploads... you're going to need to loop itforeach($_FILES['images']['error'] as $iError)and then check each$iError === UPLOAD_ERR_OK;)