init
This commit is contained in:
56
back/application/api/controller_74GYL3/Upload.php
Normal file
56
back/application/api/controller_74GYL3/Upload.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
use DateTime;
|
||||
use think\Config;
|
||||
use think\Controller;
|
||||
use think\Db;
|
||||
use think\Request;
|
||||
use think\Cache;
|
||||
|
||||
header("Access-Control-Allow-Origin:*");
|
||||
header("Access-Control-Allow-Methods:GET, POST, OPTIONS, DELETE");
|
||||
header("Access-Control-Allow-Headers:DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type, Accept-Language, Origin, Accept-Encoding");
|
||||
|
||||
class Upload extends Common
|
||||
{
|
||||
|
||||
public function uploadFile()
|
||||
{
|
||||
$file = request()->file('file'); // 获取上传的文件
|
||||
if ($file == null) {
|
||||
return json(array('code' => 1, 'msg' => '未上传文件'));
|
||||
}
|
||||
|
||||
// 获取文件后缀
|
||||
$temp = explode(".", $_FILES["file"]["name"]);
|
||||
$extension = end($temp);
|
||||
|
||||
// 判断文件是否合法
|
||||
$allowedExtensions = array("jpg", "jpeg", "png", "pdf","xls","xlsx","pptx","rar","zip","docx","doc","ppt");
|
||||
if (!in_array(strtolower($extension), $allowedExtensions)) {
|
||||
return json(array('code' => 1, 'msg' => '上传文件格式错误'));
|
||||
}
|
||||
|
||||
// 移动文件到指定目录
|
||||
$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
|
||||
if ($info) {
|
||||
$filePath = '/uploads/' . $info->getSaveName();
|
||||
// $filePath = Config::get('file_server_url') . '/uploads/' . $info->getSaveName();
|
||||
// $fileName = $info->getFilename();
|
||||
// $fileType = $info->getMime();
|
||||
// $fileSize = $info->getSize();
|
||||
// $fileData = file_get_contents($info->getPathname());
|
||||
|
||||
if ($filePath) {
|
||||
return json(array('code' => 0, 'msg' => '文件上传成功', 'url' => $filePath));
|
||||
}
|
||||
} else {
|
||||
// 上传失败获取错误信息
|
||||
return json(array('code' => 1, 'msg' => $file->getError()));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user