NaughtySquid
NaughtySquid

Reputation: 2097

Check if a file is in a html upload form?

Okay I can't find a good answer to this, how do i check if a file is actually put into a file upload html form with php?

Doing

if (isset($_FILES['new_image']))

Always returns that it's set so that's no good. Any idea?

Upvotes: 1

Views: 78

Answers (3)

adamdehaven
adamdehaven

Reputation: 5920

All you need is the code below:

if(isset($_FILES['new_image'])) {
    // code to execute
}

Or alternatively,

if(isset($_FILES['new_image']) && $_FILES['new_image']['size'] > 0) {
    // code to execute
}

...and your form tag should include at least

<form enctype="multipart/form-data">

</form>

Upvotes: 3

n1xx1
n1xx1

Reputation: 2076

In php you can check if a string is a image or not with GD's imagecreatefromstring.

This function returns false if the image isn't supported, so you can check if the image is valid also.

Upvotes: 0

Lamonde
Lamonde

Reputation: 138

You could probably add and && $_FILES['new_image']['name'] != ''

Upvotes: 0

Related Questions