记一次yii2 上传文件
作者:互联网
1 view渲染
<form action="../src/website/import/report-flow" method="post" enctype="multipart/form-data"> <input type="file" name="fileExcel" id="fileExcel" /> <br /> <input type="submit" name="submit" value="Submit" /> </form>
2 上传接口
/** * @author Anana * 上传商销/盒 文件 异步处理 */ public function actionReportFlow() { $uploadFile = UploadedFile::getInstanceByName ( 'fileExcel' ); if($uploadFile){ $fileExt = $uploadFile->getExtension (); // 文件后缀 $size = $uploadFile->size;//文件大小 if (! in_array ( $fileExt, array('xlsx') )) { $reason [] = '上传的文件后缀名必须为xlsx;'; }else if($size > 20*1024*1024){ $reason [] = '文件大小不能超过20M;'; }else{ $yyyymm = date('Ym'); $fileName = $yyyymm.'.'.$fileExt; $companyCode = \Yii::$app->session['companyRow']['code']; $buildsDir = dirname(\yii::$app->basePath)."/builds/{$companyCode}/{$fileExt}/report_flow_month"; //创建生成目录文件夹 if(!file_exists($buildsDir)){ mkdir($buildsDir,0777,true); } $csvPath = $buildsDir.'/'.$fileName; $rs = $uploadFile->saveAs($csvPath); if($rs){ parent::apiReturn ( 0, [ ], '上传成功' ); }else{ $reason [] = '上传文件失败' . $uploadFile->error; } } $jsonData = [ ]; if ($reason) { $jsonData ['errorList'] = implode ( ' ', $reason ); parent::apiReturn ( 1, $jsonData, '错误信息提示' ); } }else{ parent::apiReturn ( 1, [ ], '上传失败' ); } }
标签:文件,uploadFile,else,fileExt,buildsDir,reason,yii2,上传 来源: https://www.cnblogs.com/polax/p/10642937.html