views:

45

answers:

3

I have a PHP form with an optional file input. If no file is chosen, and I do print_r($_FILES) after submission, I get this:

Array ( [the_file] => Array (
  [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) 
)

On forms with no file input, the $_FILES array is just empty, which is what I would prefer to happen if the optional file attachment input isn't used. After all, the "errors" here are really not errors - the user didn't intend to submit a file.

Is there a way to prevent a blank file input from being submitted, apart from reworking the form to use AJAX?

+3  A: 

Error condition 4 corresponds to UPLOAD_ERR_NO_FILE, which means no file was uploaded. Can't you just check for that, and if so, ignore the data in $_FILES?

Dominic Rodger
Ah - I thought that meant 4 errors. :) I wonder why they don't put the actual error message there instead of a code?
Nathan Long
A: 

You could use a client script to remove the file input from the form on submit if it is unused.

fsb
A: 

Use a simple javascript on the main Uploading form , or use php check function for file sent by user!!

Ahsan