isset 和 !empty 未通过对上传文件的检查
我有一个上传表单,其中包含要上传的文件。我遇到的问题是,即使没有上传文件, if(isset($_FILES)) OR if(!empty($_FILES)) 仍然成功通过:
$_FILES = $HTTP_POST_FILES;
if($_POST['type'] == 'photo' && isset($_FILES)){
// returns true even if no file is uploaded. What am I missing!
}
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!
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为一个超全局变量,
$_FILES
大概总是被设置,无论上传的文件是否存在。检查您期望的文件上传并查看大小字段。 (显然根据手册中的用户贡献注释,如果表单包含上传元素,即使没有上传元素,即使
isset($_FILES["my_file_name"])
也有可能返回 true :这应该可靠地工作:
(isset()是为了防止“未定义的索引”通知。)
顺便说一下,你这样做是为了什么呢?
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:
(the isset() is to prevent a "undefined index" notice.)
What do you do this for, by the way?: