使用UploadFileInterface上传图像

时间:2018-10-26 07:45:02

标签: php

此功能用于验证文件扩展名是否可接受。 运行时显示错误。请帮助我。

错误显示在字符串上调用成员函数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;
}

0 个答案:

没有答案