1

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.';
}
Gajus
  • 69,002
  • 70
  • 275
  • 438
Kim
  • 1,128
  • 6
  • 21
  • 41

2 Answers2

3

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

Nikhil Mohan
  • 891
  • 6
  • 13
1

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

Community
  • 1
  • 1
sk8terboi87 ツ
  • 3,396
  • 3
  • 34
  • 45