mirror of
https://gitee.com/apiadmin/ApiAdmin.git
synced 2025-04-06 03:58:00 +08:00
modified 新增文件上传
This commit is contained in:
parent
7fe8a5245f
commit
90a1b6d034
@ -3,8 +3,51 @@
|
||||
namespace app\admin\controller;
|
||||
|
||||
|
||||
use app\util\ReturnCode;
|
||||
|
||||
class Index extends Base {
|
||||
public function index() {
|
||||
return json(['welcome']);
|
||||
}
|
||||
|
||||
public function upload() {
|
||||
$path = '/upload/' . date('Ymd', time()) . '/';
|
||||
$name = $_FILES['file']['name'];
|
||||
$tmp_name = $_FILES['file']['tmp_name'];
|
||||
$error = $_FILES['file']['error'];
|
||||
//过滤错误
|
||||
if ($error) {
|
||||
switch ($error) {
|
||||
case 1 :
|
||||
$error_message = '您上传的文件超过了PHP.INI配置文件中UPLOAD_MAX-FILESIZE的大小';
|
||||
break;
|
||||
case 2 :
|
||||
$error_message = '您上传的文件超过了PHP.INI配置文件中的post_max_size的大小';
|
||||
break;
|
||||
case 3 :
|
||||
$error_message = '文件只被部分上传';
|
||||
break;
|
||||
case 4 :
|
||||
$error_message = '文件不能为空';
|
||||
break;
|
||||
default :
|
||||
$error_message = '未知错误';
|
||||
}
|
||||
die($error_message);
|
||||
}
|
||||
$arr_name = explode('.', $name);
|
||||
$hz = array_pop($arr_name);
|
||||
$new_name = md5(time() . uniqid()) . '.' . $hz;
|
||||
if (!file_exists($_SERVER['DOCUMENT_ROOT'] . $path)) {
|
||||
mkdir($_SERVER['DOCUMENT_ROOT'] . $path, 0755, true);
|
||||
}
|
||||
if (move_uploaded_file($tmp_name, $_SERVER['DOCUMENT_ROOT'] . $path . $new_name)) {
|
||||
return $this->buildSuccess([
|
||||
'fileName' => $new_name,
|
||||
'fileUrl' => $this->request->domain() . $path . $new_name
|
||||
]);
|
||||
} else {
|
||||
return $this->buildFailed(ReturnCode::FILE_SAVE_ERROR, '文件上传失败');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -11,9 +11,13 @@ return [
|
||||
'admin/Login/index',
|
||||
['method' => 'post']
|
||||
],
|
||||
'Index/upload' => [
|
||||
'admin/Index/upload',
|
||||
['method' => 'post', 'after_behavior' => ['\app\admin\behavior\ApiAuth', '\app\admin\behavior\AdminLog']]
|
||||
],
|
||||
'Login/logout' => [
|
||||
'admin/Login/logout',
|
||||
['method' => 'get', 'after_behavior' => $afterBehavior]
|
||||
['method' => 'get', 'after_behavior' => ['\app\admin\behavior\ApiAuth', '\app\admin\behavior\AdminLog']]
|
||||
],
|
||||
'Menu/index' => [
|
||||
'admin/Menu/index',
|
||||
|
Loading…
x
Reference in New Issue
Block a user