上传excel文件,怎么解决wps的excel验证不通过的问题 [ 2.0 版本 ]
上传文件处理的UploadForm.php
<?php
namespace common\models;
use yii\base\Model;
use common\helpers\CommonFun;
class UploadForm extends Model
{
const UPLOAD_FILE = 'file';//上传文件
const UPLOAD_IMAGE = 'image';//上传图片
public $inputFile;
public $imageFile;
public function scenarios()
{
return [
self::UPLOAD_FILE => ['inputFile'],
self::UPLOAD_IMAGE => ['imageFile'],
];
}
public function rules()
{
return [
[
['inputFile'],
'file',
'skipOnEmpty' => false,
'extensions' => ['txt', 'zip', 'pdf', 'doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx', '7z', 'gz'],
'on' => 'file'
],
[
['imageFile'],
'image',
'skipOnEmpty' => false,
'extensions' => 'png, jpg, gif, jpeg',
'on' => 'image'
],
];
}
/**
* upload file
* @author
* @dateTime 2017-05-04T19:28:13+0800
* @param string $target the upload directory
* @param string $fileName the uploaded file name
* @return mix
*/
public function upload($target = NULL,$fileName = NULL)
{
if ($this->validate()) {
//$target = $target ? $target : Yii::getAlias('@uploads') . '/'. date("Ym");
if ($this->inputFile != NULL) {
$extension = $this->inputFile->extension;
} else {
$extension = $this->imageFile->extension;
}
$target = $target ? $target : $this->setDefaultUploadDir($extension);
$dir = getcwd() . '/' . $target;
if(!file_exists($target)) {
CommonFun::recursionMkDir($target);
}
$fileName = $fileName ? $fileName : CommonFun::createUniqueStr();
$saveName = $dir . '/' . $fileName . '.' . $extension;
if($this->inputFile != NULL) {
$this->inputFile->saveAs($saveName);
} else {
$this->imageFile->saveAs($saveName);
}
$resultUrl = $target . '/' . $fileName . '.' . $extension;
return $resultUrl;
} else {
return false;
}
}
/**
* 设置默认的上传目录
* 根据各个应用生成目录
* @author
* @dateTime 2017-05-24T09:21:38+0800
* @return string 上传路径
*/
public function setDefaultUploadDir($extension = NULL)
{
$uploadDir = "uploads/" . date("Ym");
if ($extension) {
$uploadDir .= '/'. $extension;
}
if(!file_exists($uploadDir)) {
CommonFun::recursionMkDir($uploadDir);
}
return $uploadDir;
}
}
上传文件为test.xlsx
wps版的mimeType为application/octet-stream
office版的mimeType为application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
yii的验证机制是根据上传文件的mimeType来匹配对应的extension列表,然而xlsx已经被使用了,我不能再加个xlsx的键值对
在不改源码的基础上怎么让wps文件通过FileValidator的验证
bzzear 补充于 2017-06-12 16:02
wps的excel表格的mimeType为application/octet-stream
在yii2/helpers/mimeTypes.php中application/octet-stream对应的key是
["bin","bpk","deploy","dist","distz","dms","dump","elc","lrf","mar","pkg","so"]
并没有xlsx,所以认为上传文件是不合法的
bzzear 补充于 2017-06-12 16:13
好的,多谢,我看看还有没有好的方法
其他 4 个回答
-
-
-
这是我抓的我mime,自己简单写了个方法,虽然受限制,但是对我项目实用
/** * 校验Excel mime类型 * @return bool */ public function checkExcelFileMime() { $excel_mime_hash = [ 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ]; if (in_array($this->excelFile->type, $excel_mime_hash, true)) { return true; } return false; }
共 1 条回复 -
bzzear
注册时间:2017-04-20
最后登录:2021-04-18
在线时长:42小时17分
最后登录:2021-04-18
在线时长:42小时17分
- 粉丝8
- 金钱3110
- 威望80
- 积分4330