Reputation: 103
I have a form were a user can enter text and also upload images. Saving the values and images works fine. If the user does not select an image, I get the error message that no file has been selected (Error 4).
How can I run the upload image part only if the user selected an image?
I have tried:
if (!empty($_FILES['files']['name'])){ Upload the Image }
if (!empty($_FILES['files'])){ Upload the Image }
if (!empty($_FILES['files']['name'])) { Upload the Image }
if ($_FILES['files']['error'] == UPLOAD_ERR_OK) { Upload the Image }
This the form:
<input type="file" name="files[]" title="Title" maxlength="10" accept="gif|jpg|jpeg|png|tiff">
Upvotes: 0
Views: 1693
Reputation: 16963
Use is_uploaded_file()
function to check if the user has uploaded any file or not, and then process inputs accordingly, like this:
if(is_uploaded_file($_FILES['files']['tmp_name'][0])){
// user has uploaded a file
}else{
// user hasn't uploaded anything
}
Above solution code is based on your name
attribute of input
tag,
<input ... name="files[]" ... />
If it was <input ... name="files" ... />
then the if
condition would be like this:
if(is_uploaded_file($_FILES['files']['tmp_name'])){
...
}else{
...
}
Sidenote: Use var_dump($_FILES);
to see the complete array structure.
Upvotes: 2