isset和!empty没有通过检查上传的文件 [英] isset and !empty not passing through a check for uploaded files
问题描述
我有一个上传表单,上传一个文件。我的问题是即使没有文件上传if(isset($ _ FILES))或者if(!empty($ _ FILES))仍然成功通过:
$ _ FILES = $ HTTP_POST_FILES;
大概总是被设置。
if($ _ POST ['type'] =='photo'&& isset($ _ FILES)){
//即使没有上传文件也会返回true。我错过了什么!
$ / code $ / $ p
解决方案无论上传的文件是否存在,c $ c> $ _ FILES
检查文件上传你会期望,看看大小字段。 (显然根据手册中的User Contributed Notes,如果表单包含upload元素,甚至可能会返回
isset($ _ FILES [my_file_name])
即使没有选择文件也是如此。
这应该可靠:
<$ c $($ _ POST ['type'] =='photo'&&
((isset($ _ FILES [my_file_name] [size])&&
($ _FILES [my_file_name] [size]> 0))){
isset()是为了防止未定义的索引的通知。)
顺便说一下,你做了什么?:
$ _ FILES = $ HTTP_POST_FILES;
I have an upload form with a file to be uploaded. The issue I have is that even when no file is uploaded the if(isset($_FILES)) OR if(!empty($_FILES)) still passes as successful:
$_FILES = $HTTP_POST_FILES; if($_POST['type'] == 'photo' && isset($_FILES)){ // returns true even if no file is uploaded. What am I missing! }
解决方案Being a superglobal,
$_FILES
is presumably always set, regardless whether an uploaded file exists or not.Check for the file upload(s) you would expect and look at the size field. (Apparently according to the User Contributed Notes in the manual, if the form contains the upload element, it is possible that even
isset($_FILES["my_file_name"])
will return true even though there was no file selected.This should work reliably:
if($_POST['type'] == 'photo' && ((isset($_FILES["my_file_name"]["size"]) && ($_FILES["my_file_name"]["size"] > 0)) ){
(the isset() is to prevent a "undefined index" notice.)
What do you do this for, by the way?:
$_FILES = $HTTP_POST_FILES;
这篇关于isset和!empty没有通过检查上传的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!