From 7a545ea88f4c41bad47aa19e10a4a8342c70fc44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=96=E5=8F=AB?= <392494244@qq.com> Date: Wed, 29 Nov 2023 15:28:26 +0800 Subject: [PATCH] 1 --- modules/admin/views/mall/goods/edit.php | 10 - modules/admin/views/store/store-user-edit.php | 7 - modules/file/controllers/UploadController.php | 351 +++++++++++------- widgets/views/pick-file.php | 4 +- 4 files changed, 211 insertions(+), 161 deletions(-) diff --git a/modules/admin/views/mall/goods/edit.php b/modules/admin/views/mall/goods/edit.php index b5d3cdd..bdb9044 100644 --- a/modules/admin/views/mall/goods/edit.php +++ b/modules/admin/views/mall/goods/edit.php @@ -165,23 +165,13 @@ $this->params['breadcrumbs'][] = $this->title; $json_de = json_decode($model->goodsHub->video_url, true); $temp_arr = []; foreach ($json_de as $key => $val) { - - - if($_SERVER['REMOTE_ADDR'] == '127.0.0.1'){ - $val_model = filesize('D:/phpstudy_pro/WWW/cxgj/web' . $val); - }else{ - $val_model = filesize($_SERVER["DOCUMENT_ROOT"] . $val); - } - $explode = explode('/', $val); $end = end($explode); // $temp_arr[] = [ 'name' => $end, 'url' => $val, - 'size_cn' => $val_model, ]; - } $model->goodsHub->video_url = json_encode($temp_arr, JSON_UNESCAPED_UNICODE); } diff --git a/modules/admin/views/store/store-user-edit.php b/modules/admin/views/store/store-user-edit.php index 697a9cd..9e73b26 100644 --- a/modules/admin/views/store/store-user-edit.php +++ b/modules/admin/views/store/store-user-edit.php @@ -103,18 +103,11 @@ $this->params['breadcrumbs'][] = $this->title; $temp_arr = []; foreach ($json_de as $key => $val) { - if ($_SERVER['REMOTE_ADDR'] == '127.0.0.1') { - $val_model = filesize('D:/phpstudy_pro/WWW/cxgj/web' . $val); - } else { - $val_model = filesize($_SERVER["DOCUMENT_ROOT"] . $val); - } $explode = explode('/', $val); $end = end($explode); - // $temp_arr[] = [ 'name' => $end, 'url' => $val, - 'size_cn' => $val_model, ]; } $model->cover_pic = json_encode($temp_arr, JSON_UNESCAPED_UNICODE); diff --git a/modules/file/controllers/UploadController.php b/modules/file/controllers/UploadController.php index 8e64e5c..1532302 100644 --- a/modules/file/controllers/UploadController.php +++ b/modules/file/controllers/UploadController.php @@ -12,12 +12,15 @@ namespace app\modules\file\controllers; +use app\components\Oss; use Yii; use app\modules\file\components\Uploader; use app\modules\file\components\UploadHelper; use app\models\file\UploadFile; use app\models\Model; + set_time_limit(0); + use Gumlet\ImageResize; use Gumlet\ImageResizeException; @@ -32,13 +35,13 @@ class UploadController extends \app\controllers\Controller /* 文件上传的根目录 */ //'rootPath' => $_SERVER['DOCUMENT_ROOT'], /* 上传图片配置项 */ - "imageActionName" => "uploadimage", + "imageActionName" => "uploadimage", /* 执行上传图片的action名称 */ - "imageFieldName" => "file", + "imageFieldName" => "file", /* 提交的图片表单名称 */ - "imageMaxSize" => 20480000, + "imageMaxSize" => 20480000, /* 上传大小限制,单位B */ - "imageAllowFiles" => [ + "imageAllowFiles" => [ ".png", ".jpg", ".jpeg", @@ -47,15 +50,15 @@ class UploadController extends \app\controllers\Controller ".bmp" ], /* 上传图片格式显示 */ - "imageCompressEnable" => true, + "imageCompressEnable" => true, /* 是否压缩图片,默认是true */ - "imageCompressBorder" => 800, + "imageCompressBorder" => 800, /* 图片压缩最长边限制 */ - "imageInsertAlign" => "none", + "imageInsertAlign" => "none", /* 插入的图片浮动方式 */ - "imageUrlPrefix" => "", + "imageUrlPrefix" => "", /* 图片访问路径前缀 */ - "imagePathFormat" => "/upload/image/{yyyy}/{mm}{dd}/{time}{rand:6}", + "imagePathFormat" => "/upload/image/{yyyy}/{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */ /* {filename} 会替换成原文件名,配置这项需要注意中文乱码问题 */ /* {rand:6} 会替换成随机数,后面的数字是随机数的位数 */ @@ -71,44 +74,44 @@ class UploadController extends \app\controllers\Controller /* 具请体看线上文档=> fex.baidu.com/ueditor/#use-format_upload_filename */ /* 涂鸦图片上传配置项 */ - "scrawlActionName" => "uploadscrawl", + "scrawlActionName" => "uploadscrawl", /* 执行上传涂鸦的action名称 */ - "scrawlFieldName" => "file", + "scrawlFieldName" => "file", /* 提交的图片表单名称 */ - "scrawlPathFormat" => "/upload/image/{yyyy}/{mm}{dd}/{time}{rand:6}", + "scrawlPathFormat" => "/upload/image/{yyyy}/{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */ - "scrawlMaxSize" => 2048000, + "scrawlMaxSize" => 2048000, /* 上传大小限制,单位B */ - "scrawlUrlPrefix" => "", + "scrawlUrlPrefix" => "", /* 图片访问路径前缀 */ - "scrawlInsertAlign" => "none", + "scrawlInsertAlign" => "none", /* 截图工具上传 */ - "snapscreenActionName" => "uploadimage", + "snapscreenActionName" => "uploadimage", /* 执行上传截图的action名称 */ - "snapscreenPathFormat" => "/upload/image/{yyyy}/{mm}{dd}/{time}{rand:6}", + "snapscreenPathFormat" => "/upload/image/{yyyy}/{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */ - "snapscreenUrlPrefix" => "", + "snapscreenUrlPrefix" => "", /* 图片访问路径前缀 */ - "snapscreenInsertAlign" => "none", + "snapscreenInsertAlign" => "none", /* 插入的图片浮动方式 */ /* 抓取远程图片配置 */ - "catcherLocalDomain" => [ + "catcherLocalDomain" => [ "127.0.0.1", "localhost", "img.baidu.com" ], - "catcherActionName" => "catchimage", + "catcherActionName" => "catchimage", /* 执行抓取远程图片的action名称 */ - "catcherFieldName" => "source", + "catcherFieldName" => "source", /* 提交的图片列表表单名称 */ - "catcherPathFormat" => "/upload/image/{yyyy}/{mm}{dd}/{time}{rand:6}", + "catcherPathFormat" => "/upload/image/{yyyy}/{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */ - "catcherUrlPrefix" => "", + "catcherUrlPrefix" => "", /* 图片访问路径前缀 */ - "catcherMaxSize" => 2048000, + "catcherMaxSize" => 2048000, /* 上传大小限制,单位B */ - "catcherAllowFiles" => [ + "catcherAllowFiles" => [ ".png", ".jpg", ".jpeg", @@ -119,17 +122,17 @@ class UploadController extends \app\controllers\Controller /* 抓取图片格式显示 */ /* 上传视频配置 */ - "videoActionName" => "uploadvideo", + "videoActionName" => "uploadvideo", /* 执行上传视频的action名称 */ - "videoFieldName" => "file", + "videoFieldName" => "file", /* 提交的视频表单名称 */ - "videoPathFormat" => "/upload/video/{yyyy}/{mm}{dd}/{time}{rand:6}", + "videoPathFormat" => "/upload/video/{yyyy}/{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */ - "videoUrlPrefix" => "", + "videoUrlPrefix" => "", /* 视频访问路径前缀 */ - "videoMaxSize" => 5120000, + "videoMaxSize" => 5120000, /* 上传大小限制,单位B,默认100MB */ - "videoAllowFiles" => [ + "videoAllowFiles" => [ ".flv", // ".swf", // ".mkv", @@ -151,17 +154,17 @@ class UploadController extends \app\controllers\Controller /* 上传视频格式显示 */ /* 上传文件配置 */ - "fileActionName" => "uploadfile", + "fileActionName" => "uploadfile", /* controller里,执行上传视频的action名称 */ - "fileFieldName" => "file", + "fileFieldName" => "file", /* 提交的文件表单名称 */ - "filePathFormat" => "/upload/file/{yyyy}/{mm}{dd}/{time}{rand:6}", + "filePathFormat" => "/upload/file/{yyyy}/{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */ - "fileUrlPrefix" => "", + "fileUrlPrefix" => "", /* 文件访问路径前缀 */ - "fileMaxSize" => 51200000, + "fileMaxSize" => 51200000, /* 上传大小限制,单位B,默认50MB */ - "fileAllowFiles" => [ + "fileAllowFiles" => [ ".png", ".jpg", ".jpeg", @@ -207,17 +210,17 @@ class UploadController extends \app\controllers\Controller /* 上传文件格式显示 */ /* 列出指定目录下的图片 */ - "imageManagerActionName" => "listimage", + "imageManagerActionName" => "listimage", /* 执行图片管理的action名称 */ - "imageManagerListPath" => "/upload/image/", + "imageManagerListPath" => "/upload/image/", /* 指定要列出图片的目录 */ - "imageManagerListSize" => 20, + "imageManagerListSize" => 20, /* 每次列出文件数量 */ - "imageManagerUrlPrefix" => "", + "imageManagerUrlPrefix" => "", /* 图片访问路径前缀 */ "imageManagerInsertAlign" => "none", /* 插入的图片浮动方式 */ - "imageManagerAllowFiles" => [ + "imageManagerAllowFiles" => [ ".png", ".jpg", ".jpeg", @@ -228,15 +231,15 @@ class UploadController extends \app\controllers\Controller /* 列出的文件类型 */ /* 列出指定目录下的文件 */ - "fileManagerActionName" => "listfile", + "fileManagerActionName" => "listfile", /* 执行文件管理的action名称 */ - "fileManagerListPath" => "/upload/file/", + "fileManagerListPath" => "/upload/file/", /* 指定要列出文件的目录 */ - "fileManagerUrlPrefix" => "", + "fileManagerUrlPrefix" => "", /* 文件访问路径前缀 */ - "fileManagerListSize" => 20, + "fileManagerListSize" => 20, /* 每次列出文件数量 */ - "fileManagerAllowFiles" => [ + "fileManagerAllowFiles" => [ ".png", ".jpg", ".jpeg", @@ -291,7 +294,7 @@ class UploadController extends \app\controllers\Controller $action = htmlspecialchars($action); switch ($action) { case 'config': - $result = json_encode($this->uploadConfig['uploadFile']); + $result = json_encode($this->uploadConfig['uploadFile']); break; /* 上传图片 */ case 'uploadimage': @@ -301,13 +304,13 @@ class UploadController extends \app\controllers\Controller case 'uploadvideo': /* 上传文件 */ case 'uploadfile': - if(isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == "OPTIONS"){ + if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == "OPTIONS") { //判断实际发送的请求是否安全 $result = ""; } else { - if(Yii::$app->request->isPost && !empty($_FILES)){ + if (Yii::$app->request->isPost && !empty($_FILES)) { //上传 - $currentFileInfo = $this->uploadFile($action); + $currentFileInfo = $this->uploadFile($action); $result = json_encode($currentFileInfo); } else { $result = ""; @@ -317,7 +320,7 @@ class UploadController extends \app\controllers\Controller /* 列出图片 */ case 'listimage': - if(isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == "OPTIONS"){ + if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == "OPTIONS") { $result = ""; } else { //返回结果 @@ -326,7 +329,7 @@ class UploadController extends \app\controllers\Controller break; /* 列出文件 */ case 'listfile': - if(isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == "OPTIONS"){ + if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == "OPTIONS") { $result = ""; } else { //返回结果 @@ -336,7 +339,7 @@ class UploadController extends \app\controllers\Controller /* 抓取远程文件 */ case 'catchimage': - if(isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == "OPTIONS"){ + if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == "OPTIONS") { $result = ""; } else { //返回结果 @@ -346,7 +349,7 @@ class UploadController extends \app\controllers\Controller default: $result = json_encode(array( - 'state'=> '请求地址出错' + 'state' => '请求地址出错' )); break; } @@ -357,7 +360,7 @@ class UploadController extends \app\controllers\Controller echo htmlspecialchars($_GET["callback"]) . '(' . $result . ')'; } else { echo json_encode(array( - 'state'=> 'callback参数不合法' + 'state' => 'callback参数不合法' )); } } else { @@ -365,64 +368,124 @@ class UploadController extends \app\controllers\Controller } } - //上传图片 - public function actionImage($rfmt = 'default') +// //上传图片 +// public function actionImage($rfmt = 'default') +// { +// if(!\Yii::$app->request->isPost){ +// $data = [ +// 'code' => 1, +// 'msg' => "Invalid Request" +// ]; +// return $this->responseHandler($data); +// } +// $action = "uploadimage"; +// $fileInfo = $this->uploadFile($action); +// if($rfmt == 'default'){ +// if($fileInfo["state"] == "SUCCESS"){ +// $data = [ +// 'code' => 0, +// 'msg' => 'success', +// 'data' => [ +// 'url' => $fileInfo['url'], +// 'extension' => $fileInfo["type"], +// 'size' => $fileInfo["size"], +// 'type' => $this->getFileType(trim($fileInfo['type'],".")) +// ] +// ]; +// } else { +// $data = [ +// 'code' => 1, +// 'msg' => $fileInfo["state"] +// ]; +// } +// } else if($rfmt == 'wangeditor'){ +// if($fileInfo["state"] == "SUCCESS"){ +// $data = [ +// 'errno' => 0, +// 'message' => 'success', +// 'data' => [ +// 'url' => $fileInfo['url'], +// 'alt' => '', +// 'href' => '', +// 'extension' => $fileInfo["type"], +// 'size' => $fileInfo["size"], +// 'type' => $this->getFileType(trim($fileInfo['type'],".")) +// ] +// ]; +// } else { +// $data = [ +// 'errno' => 1, +// 'message' => $fileInfo["state"] +// ]; +// } +// } +// return $this->responseHandler($data); +// } + + + //上传图片到OSS + public function actionImage() { - if(!\Yii::$app->request->isPost){ + if (!\Yii::$app->request->isPost) { $data = [ 'code' => 1, 'msg' => "Invalid Request" ]; return $this->responseHandler($data); } - $action = "uploadimage"; - $fileInfo = $this->uploadFile($action); - if($rfmt == 'default'){ - if($fileInfo["state"] == "SUCCESS"){ + + if (empty($_FILES['file']['name'])) { + $data = [ + 'code' => 1, + 'msg' => "请选择要上传的文件" + ]; + return $this->responseHandler($data); + } + + + // 上传文件的目录 + $upload_dir = "/web/uploads/1/"; + + if (!is_dir($upload_dir)) { + mkdir(\Yii::$app->basePath . '/web/upload/1', 0777, true); + } + $file = md5($_FILES['file']['name']); + // 上传文件的全名 + $upload_file = \Yii::$app->basePath . '/web/upload/1/' . $file . '.png'; + // 将上传的文件从临时目录移动到指定目录 + if (!move_uploaded_file($_FILES['file']['tmp_name'], $upload_file)) { + $data = [ + 'code' => 1, + 'msg' => "文件上传失败" + ]; + } else { + $oss = new Oss(); + $file = '/web/upload/1/' . $file . '.png'; + //上传文件目录 + $oss_data = $oss->upload($upload_file, $file); + if ($oss == false) { + $data = [ + 'code' => 1, + 'msg' => "文件上传失败" + ]; + } else { $data = [ 'code' => 0, 'msg' => 'success', 'data' => [ - 'url' => $fileInfo['url'], - 'extension' => $fileInfo["type"], - 'size' => $fileInfo["size"], - 'type' => $this->getFileType(trim($fileInfo['type'],".")) + 'url' => $oss_data, ] ]; - } else { - $data = [ - 'code' => 1, - 'msg' => $fileInfo["state"] - ]; - } - } else if($rfmt == 'wangeditor'){ - if($fileInfo["state"] == "SUCCESS"){ - $data = [ - 'errno' => 0, - 'message' => 'success', - 'data' => [ - 'url' => $fileInfo['url'], - 'alt' => '', - 'href' => '', - 'extension' => $fileInfo["type"], - 'size' => $fileInfo["size"], - 'type' => $this->getFileType(trim($fileInfo['type'],".")) - ] - ]; - } else { - $data = [ - 'errno' => 1, - 'message' => $fileInfo["state"] - ]; } } return $this->responseHandler($data); } + //上传文件 public function actionFile($rfmt = 'default') { - if(!\Yii::$app->request->isPost){ + if (!\Yii::$app->request->isPost) { $data = [ 'code' => 1, 'msg' => "Invalid Request" @@ -431,8 +494,8 @@ class UploadController extends \app\controllers\Controller } $action = "uploadfile"; $fileInfo = $this->uploadFile($action); - if($rfmt == 'default'){ - if($fileInfo["state"] == "SUCCESS"){ + if ($rfmt == 'default') { + if ($fileInfo["state"] == "SUCCESS") { $data = [ 'code' => 0, 'msg' => 'success', @@ -440,7 +503,7 @@ class UploadController extends \app\controllers\Controller 'url' => $fileInfo['url'], 'extension' => $fileInfo["type"], 'size' => $fileInfo["size"], - 'type' => $this->getFileType(trim($fileInfo['type'],".")) + 'type' => $this->getFileType(trim($fileInfo['type'], ".")) ] ]; } else { @@ -449,8 +512,8 @@ class UploadController extends \app\controllers\Controller 'msg' => $fileInfo["state"] ]; } - } else if($rfmt == 'wangeditor'){ - if($fileInfo["state"] == "SUCCESS"){ + } else if ($rfmt == 'wangeditor') { + if ($fileInfo["state"] == "SUCCESS") { $data = [ 'errno' => 0, 'message' => 'success', @@ -460,7 +523,7 @@ class UploadController extends \app\controllers\Controller 'href' => '', 'extension' => $fileInfo["type"], 'size' => $fileInfo["size"], - 'type' => $this->getFileType(trim($fileInfo['type'],".")) + 'type' => $this->getFileType(trim($fileInfo['type'], ".")) ] ]; } else { @@ -476,7 +539,7 @@ class UploadController extends \app\controllers\Controller //上传视频 public function actionVideoFile() { - if(!\Yii::$app->request->isPost){ + if (!\Yii::$app->request->isPost) { $data = [ 'code' => 1, 'msg' => "Invalid Request" @@ -484,15 +547,15 @@ class UploadController extends \app\controllers\Controller } $action = "uploadvideo"; $fileInfo = $this->uploadFile($action); - if($fileInfo["state"] == "SUCCESS"){ - $data = [ + if ($fileInfo["state"] == "SUCCESS") { + $data = [ 'code' => 0, 'msg' => 'success', 'data' => [ 'url' => $fileInfo['url'], 'extension' => $fileInfo["type"], 'size' => $fileInfo["size"], - 'type' => $this->getFileType(trim($fileInfo['type'],".")) + 'type' => $this->getFileType(trim($fileInfo['type'], ".")) ] ]; } else { @@ -509,7 +572,8 @@ class UploadController extends \app\controllers\Controller * @TODO 扩展上传至第三方存储 * 文件上传 */ - private function uploadFile($action){ + private function uploadFile($action) + { //获取上传配置 $uploadConfig = $this->getUploadConfig($action); $path = $uploadConfig["path"]; @@ -574,13 +638,13 @@ class UploadController extends \app\controllers\Controller */ /* 返回数据 */ - $fileInfo = $up->getFileInfo(); - if($fileInfo["state"] == "SUCCESS"){ + $fileInfo = $up->getFileInfo(); + if ($fileInfo["state"] == "SUCCESS") { $this->imageCompress($fileInfo); $model = new UploadFile(); $model->user_id = Model::getUserId(); $model->file_url = $fileInfo["url"]; - $model->extension = trim($fileInfo["type"],"."); + $model->extension = trim($fileInfo["type"], "."); $model->type = $this->getFileType($model->extension); $model->size = $fileInfo["size"]; $model->group_id = \Yii::$app->request->get("group_id"); @@ -592,18 +656,18 @@ class UploadController extends \app\controllers\Controller private function imageCompress(&$fileInfo) { - $filetype = $this->getFileType(trim($fileInfo["type"],".")); - if($filetype != "image") + $filetype = $this->getFileType(trim($fileInfo["type"], ".")); + if ($filetype != "image") return; $imageCompressConfig = $this->getImageCompressConfig(); - $filename = trim($fileInfo["url"],'/'); - if($imageCompressConfig['imageCompressEnable'] && file_exists($filename)){ - try{ + $filename = trim($fileInfo["url"], '/'); + if ($imageCompressConfig['imageCompressEnable'] && file_exists($filename)) { + try { $gumletImagic = new ImageResize($filename); $gumletImagic->resizeToLongSide($imageCompressConfig['imageCompressBorder']); $gumletImagic->save($filename); clearstatcache(); - } catch (ImageResizeException $ex){ + } catch (ImageResizeException $ex) { } } $fileInfo["size"] = filesize($filename); @@ -618,14 +682,14 @@ class UploadController extends \app\controllers\Controller { $config = []; $imageCompressEnable = \Yii::$app->request->get('imageCompressEnable'); - if($imageCompressEnable !== null){ + if ($imageCompressEnable !== null) { $config['imageCompressEnable'] = $imageCompressEnable == 1 ? true : false; } else { $config['imageCompressEnable'] = $this->uploadConfig['uploadFile']['imageCompressEnable']; } $imageCompressBorder = \Yii::$app->request->get('imageCompressBorder'); - if($imageCompressBorder !== null && is_numeric($imageCompressBorder)){ + if ($imageCompressBorder !== null && is_numeric($imageCompressBorder)) { $imageCompressBorder = (int)$imageCompressBorder; $config['imageCompressBorder'] = $imageCompressBorder; } else { @@ -651,7 +715,8 @@ class UploadController extends \app\controllers\Controller /** * 获取已经上传的文件列表 */ - private function getFileList($action){ + private function getFileList($action) + { //获取上传配置 $uploadConfig = $this->getUploadConfig($action); $_path = $uploadConfig["path"]; @@ -661,7 +726,7 @@ class UploadController extends \app\controllers\Controller switch ($action) { /* 列出文件 */ case 'listfile': - $_config['fileManagerListPath'] = $_path .$_config['fileManagerListPath']; + $_config['fileManagerListPath'] = $_path . $_config['fileManagerListPath']; $allowFiles = $_config['fileManagerAllowFiles']; $listSize = $_config['fileManagerListSize']; $path = $_config['fileManagerListPath']; @@ -682,7 +747,7 @@ class UploadController extends \app\controllers\Controller $end = $start + $size; /* 获取文件列表 */ - $path = $_SERVER['DOCUMENT_ROOT'] . (substr($path, 0, 1) == "/" ? "":"/") . $path; + $path = $_SERVER['DOCUMENT_ROOT'] . (substr($path, 0, 1) == "/" ? "" : "/") . $path; $files = $this->getfiles($path, $allowFiles); if (!count($files)) { return json_encode(array( @@ -695,7 +760,7 @@ class UploadController extends \app\controllers\Controller /* 获取指定范围的列表 */ $len = count($files); - for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--){ + for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--) { $list[] = $files[$i]; } //倒序 @@ -723,7 +788,7 @@ class UploadController extends \app\controllers\Controller private function getfiles($path, $allowFiles, &$files = array()) { if (!is_dir($path)) return null; - if(substr($path, strlen($path) - 1) != '/') $path .= '/'; + if (substr($path, strlen($path) - 1) != '/') $path .= '/'; $handle = opendir($path); while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') { @@ -731,10 +796,10 @@ class UploadController extends \app\controllers\Controller if (is_dir($path2)) { $this->getfiles($path2, $allowFiles, $files); } else { - if (preg_match("/\.(".$allowFiles.")$/i", $file)) { + if (preg_match("/\.(" . $allowFiles . ")$/i", $file)) { $files[] = array( - 'url'=> substr($path2, strlen($_SERVER['DOCUMENT_ROOT'])), - 'mtime'=> filemtime($path2) + 'url' => substr($path2, strlen($_SERVER['DOCUMENT_ROOT'])), + 'mtime' => filemtime($path2) ); } } @@ -746,7 +811,8 @@ class UploadController extends \app\controllers\Controller /** * 获取上传路径前缀,及配置信息 */ - private function getUploadConfig($action){ + private function getUploadConfig($action) + { $path = UploadHelper::getUploadPath(); $config = $this->uploadConfig['uploadFile']; @@ -760,7 +826,8 @@ class UploadController extends \app\controllers\Controller /** * 抓取远程图片 */ - private function crawlerImage($action) { + private function crawlerImage($action) + { set_time_limit(0); //获取上传配置 $uploadConfig = $this->getUploadConfig($action); @@ -798,29 +865,29 @@ class UploadController extends \app\controllers\Controller /* 返回抓取数据 */ return json_encode(array( - 'state'=> count($list) ? 'SUCCESS': 'ERROR', - 'list'=> $list + 'state' => count($list) ? 'SUCCESS' : 'ERROR', + 'list' => $list )); } public function actionVideo() { - set_time_limit (0); + set_time_limit(0); $params = \Yii::$app->request->post(); - $tempDir = './temp/upload/video/chunks/'.date('Ymd',time()) . '/' . md5($params['name']); - if(!file_exists($tempDir)) + $tempDir = './temp/upload/video/chunks/' . date('Ymd', time()) . '/' . md5($params['name']); + if (!file_exists($tempDir)) $this->mk_dir($tempDir); - if(!isset($params['chunk'])){ + if (!isset($params['chunk'])) { $params['chunk'] = 0; } - if(!isset($params['chunks'])){ + if (!isset($params['chunks'])) { $params['chunks'] = 1; } //获取上传配置 $action = 'uploadvideo'; $uploadConfig = $this->getUploadConfig($action); $config = array( - "pathFormat" => $tempDir . '/' .$params['chunk'], + "pathFormat" => $tempDir . '/' . $params['chunk'], "maxSize" => $uploadConfig['config']['videoMaxSize'], "allowFiles" => $uploadConfig['config']['videoAllowFiles'] ); @@ -830,17 +897,17 @@ class UploadController extends \app\controllers\Controller $fileInfo = $up->getFileInfo(); $chunk = strval($params['chunk']); $chunkCount = strval($params['chunks']); - if($chunk + 1 == $chunkCount){ + if ($chunk + 1 == $chunkCount) { //合并 - $targetDir = './upload/video/'.date('Ymd',time()). '/' . md5($params['name']); - if(!file_exists($targetDir)) + $targetDir = './upload/video/' . date('Ymd', time()) . '/' . md5($params['name']); + if (!file_exists($targetDir)) $this->mk_dir($targetDir); $finalName = $targetDir . $fileInfo['type']; - if(file_exists($finalName)) + if (file_exists($finalName)) unlink($finalName); $file = fopen($finalName, 'wb'); - for($index = 0; $index < $chunkCount; $index++){ - $tmpFile = $tempDir . '/'. $index . $fileInfo['type']; + for ($index = 0; $index < $chunkCount; $index++) { + $tmpFile = $tempDir . '/' . $index . $fileInfo['type']; $chunkFile = fopen($tmpFile, 'rb'); $content = fread($chunkFile, filesize($tmpFile)); fclose($chunkFile); @@ -859,8 +926,8 @@ class UploadController extends \app\controllers\Controller return $fileInfo; } - private function mk_dir($dir,$mode = 0777) + private function mk_dir($dir, $mode = 0777) { - return is_dir($dir) or $this->mk_dir(dirname($dir),$mode) and mkdir($dir,$mode); + return is_dir($dir) or $this->mk_dir(dirname($dir), $mode) and mkdir($dir, $mode); } } \ No newline at end of file diff --git a/widgets/views/pick-file.php b/widgets/views/pick-file.php index e2f1763..ff9526d 100644 --- a/widgets/views/pick-file.php +++ b/widgets/views/pick-file.php @@ -109,7 +109,7 @@ $this->registerJsFile($layui_js_file, ['depends' => 'app\modules\admin\assets\Ap 名称 文件 - 大小 + 状态 操作 @@ -118,7 +118,7 @@ $this->registerJsFile($layui_js_file, ['depends' => 'app\modules\admin\assets\Ap {{item.name}} {{item.url}} - {{item.size_cn}} +