增加上传文件模块
This commit is contained in:
parent
e62965a7ab
commit
347c90afb2
|
@ -0,0 +1,41 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace app\admin\controller;
|
||||||
|
|
||||||
|
use app\model\Admins;
|
||||||
|
use app\model\Uploads;
|
||||||
|
use app\model\Backs;
|
||||||
|
use app\model\Finances;
|
||||||
|
use app\model\Onlines;
|
||||||
|
use app\model\Orders;
|
||||||
|
use DateTime;
|
||||||
|
use Qiniu\Auth;
|
||||||
|
use support\Log;
|
||||||
|
use support\Redis;
|
||||||
|
use support\Request;
|
||||||
|
|
||||||
|
class UploadController extends base
|
||||||
|
{
|
||||||
|
public function index(Request $request)
|
||||||
|
{
|
||||||
|
$file = $request->file("file");
|
||||||
|
|
||||||
|
if (!$file->isValid()) {
|
||||||
|
return $this->error(400, 'upload fail, code=' . $file->getUploadErrorCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
$now = date_create();
|
||||||
|
$savepath = sprintf("/uploads/%d/%s/%s", $request->admin->id, $now->format("YmdHisu"), $file->getUploadName());
|
||||||
|
$file->move(public_path(). $savepath);
|
||||||
|
|
||||||
|
$item = new Uploads();
|
||||||
|
$item->admin_id = $request->admin->id;
|
||||||
|
$item->filesize = $file->getSize();
|
||||||
|
$item->filepath = $savepath;
|
||||||
|
$item->mime = $file->getUploadMimeType();
|
||||||
|
$item->create_at = $now->getTimestamp();
|
||||||
|
$item->save();
|
||||||
|
|
||||||
|
return $this->success($savepath);
|
||||||
|
}
|
||||||
|
}
|
|
@ -21,7 +21,8 @@ class adminAuth implements MiddlewareInterface
|
||||||
//白名单
|
//白名单
|
||||||
$url = [
|
$url = [
|
||||||
'/admin/login',
|
'/admin/login',
|
||||||
'admin/index/avatar'
|
'admin/index/avatar',
|
||||||
|
'admin/upload/index',
|
||||||
];
|
];
|
||||||
if(in_array($path, $url)) {
|
if(in_array($path, $url)) {
|
||||||
return $next($request);
|
return $next($request);
|
||||||
|
|
|
@ -0,0 +1,36 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace app\model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* uploads 上传文件
|
||||||
|
* @property integer $id (主键)
|
||||||
|
* @property integer $admin_id 谁上传的
|
||||||
|
* @property integer $filesize 文件大小
|
||||||
|
* @property string $filepath 存储路径
|
||||||
|
* @property string $mime mime 类型
|
||||||
|
* @property integer $create_at 上传时间
|
||||||
|
*/
|
||||||
|
class Uploads extends base
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The connection name for the model.
|
||||||
|
*
|
||||||
|
* @var string|null
|
||||||
|
*/
|
||||||
|
protected $connection = 'mysql';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The table associated with the model.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $table = 'uploads';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The primary key associated with the table.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $pk = 'id';
|
||||||
|
}
|
Loading…
Reference in New Issue