/
Upload.php
125 lines (113 loc) · 3.71 KB
/
Upload.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace crazyfd\ueditor;
use Yii;
use yii\base\Action;
use yii\web\UploadedFile;
use yii\helpers\Json;
class Upload extends Action
{
public $currentPath;
public $config = [
];
public $action;
public $callback;
public function init()
{
$this->currentPath = dirname(__FILE__);
Yii::$app->request->enableCsrfValidation = false;
Yii::$app->request->enableCookieValidation = true;
$this->config = array_merge([
'uploadDir' => date('Y/m/d'),
'webroot'=>Yii::getAlias('@webroot'),
],$this->config);
parent::init();
}
/**
* Runs the action.
*/
public function run()
{
$action = Yii::$app->getRequest()->get('action', null);
switch ($action) {
case 'config':
$result = Json::encode($this->config());
break;
/* 上传图片 */
case 'uploadimage':
/* 上传涂鸦 */
case 'uploadscrawl':
/* 上传视频 */
case 'uploadvideo':
/* 上传文件 */
case 'uploadfile':
$result = $this->fileUpload();
break;
/* 抓取远程文件 */
case 'catchimage':
$result = $this->saveRemote();
break;
default:
$result = Json::encode(array('state' => '请求地址出错'));
break;
}
/* 输出结果 */
$callback = Yii::$app->getRequest()->get('callback', null);
if ($callback) {
if (preg_match("/^[\w_]+$/", $callback)) {
$result = htmlspecialchars($callback) . '(' . $result . ')';
} else {
$result = Json::encode(array('state' => 'callback参数不合法'));
}
}
return $result;
}
protected function fileUpload($config = [])
{
//待完善
// $ak = Yii::$app->params['qiniu']['ak'];
// $sk = Yii::$app->params['qiniu']['sk'];
// $domain = Yii::$app->params['qiniu']['domain'];
// $bucket = Yii::$app->params['qiniu']['bucket'];
// $qiniu = new \crazyfd\qiniu\Qiniu($ak, $sk,$domain, $bucket);
//
// foreach($_FILES as $file){
// $ext = strtolower(strrchr($file['name'], '.'));
// $key = date('Y/m/').uniqid().mt_rand(1000,9999).$ext;
// $qiniu->uploadFile($file['tmp_name'],$key);
// }
// $url = $qiniu->getLink($key);
$file = UploadedFile::getInstanceByName('upfile');
$webroot = $this->config['webroot'];
$dir = $this->config['uploadDir'];
if (!file_exists($webroot.'/'.$dir)){
self::makeDir($webroot.'/'.$dir);
}
$name = '/'.$dir.'/' . time().mt_rand(100,999) . '.' . $file->extension;
$file->saveAs($webroot.$name);
return Json::encode(['state' => 'SUCCESS', 'url' => Yii::$app->urlManager->getBaseUrl().$name, "size" =>$file->size, "original" => $file->name, "name" => $file->name]);
}
protected function saveRemote($config = [])
{
//待完善
}
private function config()
{
return Json::decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents($this->currentPath . '/config.json')), true);
}
public static function makeDir($dir)
{
if (!is_dir($dir)) {
if (!is_dir(dirname($dir))) {
self::makeDir(dirname($dir));
mkdir($dir, 0777);
} else {
mkdir($dir, 0777);
}
}
}
}