1

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?

1
  • What problem are you trying to solve? If the user didn't choose a file, why not just ignore $_FILES entirely? Commented Jan 27, 2010 at 15:18

3 Answers 3

4

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?

Sign up to request clarification or add additional context in comments.

1 Comment

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

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

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.