Kim
Kim

Reputation: 1156

How to check if a file was uploaded?

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

Answers (2)

sk8terboi87  ツ
sk8terboi87 ツ

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

Nikhil Mohan
Nikhil Mohan

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

Related Questions