ctms/pms-admin/controllers/UploadController.php
2025-04-10 23:19:13 +08:00

180 lines
5.8 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* @Author: fm453
* @Date: 2018-08-17 19:39:41
* @Last Modified by: fm453
* @Last Modified time: 2021-04-28 17:30:36
* @Email: fm453@lukegzs.com
*/
namespace backend\controllers;
use Yii;
use yii\data\Pagination;
use yii\helpers\Url;
use backend\models\CAttachment;
use common\components\Attachment;
class UploadController extends \yii\web\Controller
{
public $enableCsrfValidation = false; //取消csrf令牌验证不取消的话文件上传会失败
//替代常规的_construct 析构函数;其他方法调用前执行
public function init()
{
parent::init();
$session = Yii::$app->session;
if(isset($_GET['pid']) && (int)$_GET['pid']>0){
$session->set('pid',(int)$_GET['pid']);
}
$pid = $session->get('pid');
if(!$pid){
$url = Url::toRoute('/index/index');
return $this->redirect($url);
}
}
//主界面
public function actionIndex()
{
return $this->render('../layouts/dev',[]);
}
public function actionImage()
{
global $_HI,$_FM;
$siteId = Yii::$app->params['siteId'];
$session = Yii::$app->session;
$pid = (int)$session->get('pid');
$model = new CAttachment();
$where = [];
$where['status_code'] = 1;
$where['type'] = 0;
$where['siteid'] = $siteId;
$where['pid'] = $pid;
//分页
$orderby = ['id' => SORT_DESC];
$Attachemnt = new Attachment();
$list = $Attachemnt->getData($model,$where,$orderby);
$pager = $Attachemnt->getPagination();
//附件网址前缀
$src = Url::base(true)."/../../upload"; //在网站根目录下使用upload/pics目录;适合使用yii框架主目录入口而非绑定子域名的情形
if($_SERVER['DOCUMENT_ROOT'].'/upload' != Yii::getAlias("@upload")){
// $src = Url::toRoute(['attach/index','img'=>'']); //换用网络加载转换的方式-反应慢
$src = Yii::$app->params['attachSrc'].Yii::$app->params['attachDir'];
}
$callback = isset($_GET['callback']) ? $_GET['callback'] : 'thumb';
return $this->render('image/list',[
'list'=>$list,
'pager' => $pager,
'src'=>$src,
'callback'=>$callback
]);
}
public function actionImage_save()
{
global $_HI,$_FM;
// $post = [
// 'name'=> "IMG_3829.JPG",
// 'path'=>"/temp",
// 'root'=> "/Users/fm453/webdev/localhost/i/upload/temp/IMG_3829.JPG",
// 'size'=> 271389,
// 'type'=> "image/jpeg",
// 'lasttime'=> 'Mon Oct 15 2018 19:43:05 GMT+0800 (中国标准时间)',
// 'ext'=> "JPG",
// 'params'=> '{type: "image/jpeg", width: 1080, height: 1080}'
// ];
$post = Yii::$app->request->post();
if(!$post['root']){
die(json_encode('没有传入文件路径'));
}
// 文件附件目录
$attachDir = Yii::getAlias('@upload'); //附件所在目录
$targetDir = "/pics"; //开始构建目标转存目录
if (!file_exists($attachDir . $targetDir)) {
@mkdir($attachDir . $targetDir);
}
$session = Yii::$app->session;
$siteId = Yii::$app->params['siteId'];
$targetDir .= '/'.$siteId;
if (!file_exists($attachDir . $targetDir)) {
@mkdir($attachDir . $targetDir);
}
$pid = (int)$session->get('pid');
$targetDir .= '/'.$pid;
if (!file_exists($attachDir . $targetDir)) {
@mkdir($attachDir . $targetDir);
}
$timeDir = date('Ym');
$targetDir .= "/".$timeDir;
if (!file_exists($attachDir . $targetDir)) {
@mkdir($attachDir . $targetDir);
}
//处理请求的文件信息
$title = isset($post['name']) ? $post['name'] : md5(TIMESTAMP).'.jpg';
//取后缀名
if($post['ext']){
$suffix = strtolower($post['ext']);
}else{
$_names = explode('.',$title);
$suffix = tolower($_names[count($_names)-1]);
}
$newName = date('dHis',TIMESTAMP).'_'.md5($title);
$root = $post['root'];
$newFile = $attachDir . $targetDir."/".$newName.".".$suffix;
$tempfile = $post['root'];
//文件移动,【完整文件名-含路径,目标目录】
rename($tempfile,$newFile);
$options = json_encode($post['params']);
//文件入库
$model = new CAttachment();
$model->siteid = $siteId;
$model->pid = $pid;
$model->uid = $_FM['user']['uid'];
$model->mid = 0;
$model->type = 0;
$model->status_code = 1;
$model->title = $title;
$model->filesize = $post['size'];
$model->route = $targetDir."/".$newName.".".$suffix;
$model->options = $options;
$model->update_at = $model->create_at = time();
$model->save();
$id = $model->attributes['id']; //获取插入后id
$return=[];
$return['msg'] = '';
$return['code'] = 0;
$return['path'] = $model->route;
if(!$id){
$return['code'] = 453100;
$return['msg'] = $model->getErrors();
}
$return['data'] = ['id'=>$id];
die(json_encode($return));
}
public function actionSimditor()
{
//针对simditor插件
global $_HI,$_FM;
// "success": true/false,
// "msg": "error message", # optional
// "file_path": "[real file path]"
//
$return=[];
$return['msg'] = '';
$return['success'] = true;
$return['file_path'] = 'http://public.hiluker.com/anchengjieshun/banner-ad-1180-300.png';
die(json_encode($return));
}
}