40 lines
1.0 KiB
PHP
40 lines
1.0 KiB
PHP
<?php
|
|
|
|
namespace app\api\controller;
|
|
|
|
use app\common\controller\Api;
|
|
use app\common\exception\UploadException;
|
|
use app\common\library\Upload;
|
|
use think\Config;
|
|
|
|
/**
|
|
* 上传接口
|
|
*/
|
|
class Coollapse extends Api
|
|
{
|
|
protected $noNeedLogin = ['*'];
|
|
protected $noNeedRight = ['*'];
|
|
|
|
/**
|
|
* 上传文件
|
|
*/
|
|
public function upload()
|
|
{
|
|
Config::set('default_return_type', 'json');
|
|
//必须设定cdnurl为空,否则cdnurl函数计算错误
|
|
Config::set('upload.cdnurl', '');
|
|
$attachment = null;
|
|
//默认普通上传文件
|
|
$file = $this->request->file('file');
|
|
try {
|
|
$md5 = md5_file($file->getPathname());
|
|
$upload = new Upload($file);
|
|
$attachment = $upload->upload();
|
|
} catch (UploadException $e) {
|
|
$this->error($e->getMessage());
|
|
}
|
|
|
|
$this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true), 'size' => $attachment->filesize, 'md5' => $md5]);
|
|
}
|
|
}
|