【发布时间】:2021-04-01 21:38:29
【问题描述】:
我正在使用 PhpOffice\PhpSpreadsheet 读取 csv 文件,例如:
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile($file);
$reader->setReadDataOnly(true);
$spreadsheet = $reader->load($file);
错误:Warning: mime_content_type(): Can only process string or stream arguments。
但它适用于 xlsx 文件。
我尝试使用 PhpOffice\PhpSpreadsheet\Reader\Csv 但它是同样的错误。
我尝试转储 $file,我看到 mimeType 是 text/csv,但是当 $file->getMimeType() 返回 text/plain。这使我无法验证上传文件(我只允许用户上传 xlsx/csv 文件)。我认为这可能导致错误无法读取 csv 文件?
编辑:这是我的错。 load() 以字符串形式接收参数,但我传递了 Symfony\Component\HttpFoundation\File\UploadFile (因为它适用于 xlsx 文件)。所以将$file 更改为$file->getPathName() 效果很好。
但是对于验证文件,我想我必须接受文本/纯文本,有人知道吗?
【问题讨论】:
标签: php phpoffice-phpspreadsheet