php 如何判断是否上传了文件、图片

假设前端有字段

复制代码
<input type="file" name="user_avatar_image"/>

php使用$_FILES进行判断

  1. 当没有文件上传时,打印$_FILES

    ^ array:1 [▼
    "user_profile_image" => array:5 [▼
    "name" => ""
    "type" => ""
    "tmp_name" => ""
    "error" => 4
    "size" => 0
    ]
    ]

  2. 当有文件上传是,打印$_FILES

    ^ array:1 [▼
    "user_profile_image" => array:5 [▼
    "name" => "软件开发原则.pptx"
    "type" => "application/vnd.openxmlformats-officedocument.presentationml.presentation"
    "tmp_name" => "/Applications/MAMP/tmp/php/phpPHWyCh"
    "error" => 0
    "size" => 3031121
    ]
    ]

  3. 假如前端没有传user_avatar_image的字段,打印$_FILES

    []

因此,可以使用error字段判断是否上传了指定的文件

复制代码
// 获取表单上传文件 字段名为user_profile_image
$field_name = 'user_profile_image';
        
if(!(array_key_exists($field_name, $_FILES) && $_FILES[$field_name]['error'] == 0)){
    $this->error('参数不足');
}
相关推荐
ππ很开心6663 分钟前
DAY 32 函数专题2:装饰器
开发语言·python
Knight_AL7 分钟前
阿里《Java 开发手册》下的对象构建与赋值规范实践
java·开发语言
lsx2024069 分钟前
SQL LIKE 操作符详解
开发语言
微爱帮监所写信寄信16 分钟前
微爱帮监狱寄信写信工具照片高清处理技术架构
开发语言·人工智能·网络协议·微信·php
报错小能手27 分钟前
数据结构 字典树
开发语言·数据结构
XLYcmy36 分钟前
高级密码生成器程序详解:专门设计用于生成基于用户个人信息的密码猜测组合
开发语言·数据结构·python·网络安全·数据安全·源代码·口令安全
独自破碎E44 分钟前
Leetcode862和至少为K的最短子数组
java·开发语言
qq_370773091 小时前
x64dbg 脚本常用命令
开发语言·x64dbg
军军君011 小时前
Three.js基础功能学习五:雾与渲染目标
开发语言·前端·javascript·学习·3d·前端框架·three
charlie1145141911 小时前
FreeRTOS:软件定时器(Software Timers)与时间管理
开发语言·笔记·学习·freertos·实时操作系统·工程