php - 使用 UploadFileInterface 上传图片
问题描述
这是验证文件扩展名是否可接受的功能。当我运行它显示错误。请帮助我。
错误显示在字符串上调用成员函数 getError()
public function extension(string $key, array $extensions): self
{
/** @var UploadedFileInterface $file */
$file = $this->getValue($key);
if ($file !== null && $file->getError() === UPLOAD_ERR_OK) {
$type = $file->getClientMediaType();
$extension = mb_strtolower(pathinfo($file->getClientFilename(), PATHINFO_EXTENSION));
$expectedType = self::MIME_TYPES[$extension] ?? null;
if (!in_array($extension, $extensions) || $expectedType !== $type) {
$this->addError($key, 'filetype', [join(',', $extensions)]);
}
}
return $this;
}
解决方案
推荐阅读
- javascript - Electron:将“全屏”标志设置为 false 不会禁用 macOS 上的全屏按钮
- c# - Mongo c#查询-聚合匹配如何通过In语句检查列表中的uid是否存在
- r - 带中断的增量序列
- python - 熊猫数据框返回无值
- sql - SUBSTRING 空值替代方案
- php - php中(bool)和(boolean)之间的区别?
- python - 如何在 Python 中为 EXR 文件将 float16 转换为 uint8
- fiware-cygnus - 如何从 Fiware Cygnus 获取数据?
- java - 将运算符添加到字符串返回值
- doctrine - 如何在 symfony 4 中获取关系数据库的 id?