开发者

How to ignore a file input on a PHP form?

开发者 https://www.devze.com 2022-12-18 18:59 出处:网络
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:

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!!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号