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('参数不足');
}

相关推荐

  1. php 如何判断是否文件图片

    2024-02-09 06:32:01       28 阅读
  2. PHP 判断文件是否存在

    2024-02-09 06:32:01       12 阅读
  3. Unity UnityWebRequest 向php后端图片文件

    2024-02-09 06:32:01       32 阅读
  4. 如何判断服务器是否被入侵

    2024-02-09 06:32:01       35 阅读
  5. 如何处理PHP中的文件和下载?

    2024-02-09 06:32:01       11 阅读
  6. PHP文件以及数据写入

    2024-02-09 06:32:01       33 阅读
  7. PHP实现阿里OSS文件

    2024-02-09 06:32:01       32 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-09 06:32:01       14 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-09 06:32:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-09 06:32:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-09 06:32:01       18 阅读

热门阅读

  1. vue3——登录校验

    2024-02-09 06:32:01       26 阅读
  2. Vue-1

    2024-02-09 06:32:01       30 阅读
  3. 12.使用Promise封装Ajax

    2024-02-09 06:32:01       35 阅读
  4. 元素显示模式

    2024-02-09 06:32:01       28 阅读
  5. Kubernetes 是什么?

    2024-02-09 06:32:01       26 阅读