首页 > 解决方案 > 如何检查没有文件通过表单上传到服务器?

问题描述

我有下一个表格:

<form action="/upload" enctype="multipart/form-data" method="POST">
      <input name="example" type="file">
      <input type="submit" value="Upload">
</form>

在服务器端,我看到上传了大小为零的文件:

-----------------------------23370864791729106148808009039
Content-Disposition: form-data; name="example"; filename=""
Content-Type: application/octet-stream


-----------------------------23370864791729106148808009039--

如何检查用户没有为此file字段选择任何内容?

标签: htmlcontent-disposition

解决方案


提交后,您可以将此代码用于您的 php 文件:

if($_FILES['example']['tmp_name'] != null){
    //Actions you needed
}

如果它返回 true,则表示该文件已被选中。


推荐阅读