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 in开发者_运维知识库put from being submitted, apart from reworking the form to use AJAX?
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
?
You could use a client script to remove the file input from the form on submit if it is unused.
Use a simple javascript on the main Uploading form , or use php check function for file sent by user!!
精彩评论