Reputation: 1156
I want to check if user has uploaded an image using the form.
I have tried:
if (empty($_FILES['txtImage'])) {
$msg = 'Opss, you forgot the image.';
}
Upvotes: 1
Views: 113
Reputation: 3457
http://www.php.net/is_uploaded_file
if(!file_exists($_FILES['myfile']['tmp_name']) || !is_uploaded_file($_FILES['myfile']['tmp_name'])) {
echo 'No upload';
}
And additionally, you can check with getimagesize(), If it returns FALSE it is not an image
https://stackoverflow.com/a/946432/1172872
Upvotes: 1
Reputation: 891
Since PHP 4.2.0, PHP returns an appropriate error code along with the file array.
So,
<?php
if ($_FILES['txtImage']['error'] === UPLOAD_ERR_NO_FILE) {
$msg .= "Opss, you forgot the image.<br>";
}
?>
http://php.net/manual/en/features.file-upload.errors.php
Upvotes: 3