此功能用于验证文件扩展名是否可接受。 运行时显示错误。请帮助我。
错误显示在字符串上调用成员函数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;
}