首页 > 解决方案 > 使用 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;
}

标签: php

解决方案


推荐阅读