Resource.php
2.1 KB
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
<?php
namespace app\common\controller;
use Qiniu\Auth;
use tinymeng\code\Generate;
class Resource
{
/**
* 生成条形码
* @param $res
* @return false|string
*/
public static function StudyBar($res){
$generate = Generate::bar();
$file_path = $generate->create($res,true,true);
$file_path = substr($file_path,strripos($file_path,"public")+6);
return $file_path;
}
/*
* 七牛云上传
*/
public function qiniu($filePath = '', $fileName = '')
{
// $filePath=ROOT_PATH.'public'.DS.'uploads'.DS.'20200224'.DS.'img1.jpg';//文件路径
// $fileName='public'.DS.'uploads'.DS.'20200224'.DS.'img1.jpg'; //七牛云上的名字
// $filePath = ROOT_PATH . 'public' . DS . 'uploads' . DS . '20200224' . DS . 'img2.jpg';//文件路径
// $fileName = 'public' . DS . 'uploads' . DS . '20200224' . DS . 'img2.jpg'; //七牛云上的名字,头部不带杠
$config = get_addon_config('qiniu');
$policy = array(
'saveKey' => $fileName,
);
$auth = new Auth($config['accessKey'], $config['secretKey']);
$token = $auth->uploadToken($config['bucket'], null, $config['expire'], $policy);
$multipart = [
['name' => 'token', 'contents' => $token],
[
'name' => 'file',
'contents' => fopen($filePath, 'r'),
'filename' => $fileName,
]
];
try {
$client = new \GuzzleHttp\Client();
$res = $client->request('POST', $config['uploadurl'], [
'multipart' => $multipart
]);
$code = $res->getStatusCode();
//成功不做任何操作
} catch (\GuzzleHttp\Exception\ClientException $e) {
// unlink($filePath);
$this->error("上传失败");
}
// dump($code);
// $url = '/';
//上传成功后将存储变更为qiniu
// $this->success("上传成功", ['url' => cdnurl($url), 'save_path' => $url]);
}
}