Chris
Chris

Reputation: 67

PHP - $_FILES issues

I have the following on a PHP processing file.

if(isset($_FILES['cv'])){
    // run function here
}

Only problem is that the function is getting all the time ... Shouldnt it only be run if a file has been input?

Upvotes: 2

Views: 256

Answers (3)

yuxhuang
yuxhuang

Reputation: 5379

If you are checking if a file is correctly uploaded, you should check:

$_FILES['cv']['error'] == UPLOAD_ERR_OK

Upvotes: 1

karim79
karim79

Reputation: 342635

The 'cv' key will always be set when a form element of the same name is present. You could test for an empty file instead. Here's one way:

if(isset($_FILES['cv']) && $_FILES['cv']['size'] > 0){
    // run function here
}

Upvotes: 3

JCOC611
JCOC611

Reputation: 19719

I think that the browser sends an empty field when the user hasn't selected a file, yet it does send it so it is set.

if(isset($_FILES['cv']) && $_FILES['cv']!=null && $_FILES['cv']!=""){
   //Execute
}

Upvotes: 0

Related Questions