赶知识网

Laravel上传Excel文件报错:No ReaderType or WriterType could be detected

2021-11-17 / 1971次点击 php/mysql/apache excel Laravel

Laravel上传Excel文件,在Windows环境下没有报错,但是发布到Linux CentOS正式服务环境报错
No ReaderType or WriterType could be detected. Make sure you either pass a valid extension to the filename or pass an explicit type,具体信息如下:

"message": "No ReaderType or WriterType could be detected. Make sure you either pass a valid extension to the filename or pass an explicit type.",

    "exception": "Maatwebsite\\Excel\\Exceptions\\NoTypeDetectedException",

    "file": "/www/wwwroot/web/vendor/maatwebsite/excel/src/Helpers/FileTypeDetector.php",

    "line": 31

将原代码

$file = $_FILES['file'];

$data = \Excel::import(new UsersImport, $file['tmp_name']);

替换为

$path1 = $this->request->file('file')->store('temp');

$path = storage_path('app').'/'.$path1;

$data = \Excel::import(new UsersImport, $path);


网上的这个解决办法可能不管用或者用不上。

这个错误真正的原因是,Maatwebsite/Excel组件的新版本,只能识别带扩展名的文件路径。

因为直接使用$file['tmp_name']得到的路径不包含扩展名,所以会报错。

所以只需要把$file['tmp_name']另存为一个带文件扩展的路径就可以了。


有用 没用

Top10

沪ICP备09053415号 © 赶知识网