Reputation: 83
I have a simple php page where you add a contact and you have the option of giving them a picture. If no picture is uploaded then the contact will be given a default picture already on the server.
The problem is that the bool test I use to check if the user uploaded a file or not ALWAYS comes back as true, even when the user hasn't uploaded a file
this is the code from my html form
<tr>
<td align="right">Contacts Photo</td>
<td><label>
<input type="file" name="fileField" id="fileField" />
</label></td>
</tr>
and this is the php I use to see if a file has been uploaded or not
$haspicture = false;
if (empty($_FILES['fileField'])){
$haspicture = false;
}
if (!empty($_FILES['fileField'])){
$haspicture = true;
}
if someone could please tell what I've been doing wrong, it'd be greatly appreciated
Upvotes: 0
Views: 548
Reputation: 522081
The elements in the $_FILES
array are always arrays themselves. That's because the form element is always submitted with the form, even if it contains no content. In other words, they're never "empty". Try var_dump($_FILES)
to see what you're getting.
You should check like this:
if (isset($_FILES['fileField']['error']) && $_FILES['fileField']['error'] === UPLOAD_ERR_OK)
See http://www.php.net/manual/en/features.file-upload.errors.php.
Upvotes: 1