123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- <?php
- /**
- * Created by PhpStorm.
- * User: sicilon_IT
- * Date: 2020/1/5
- * Time: 9:49
- */
- namespace app\index\controller;
- use think\Controller;
- use think\facade\Log;
- use think\Request;
- class Fileoper extends Controller
- {
- private function filterfiletype($usefile = "file")
- {
- $filetype = $_FILES[$usefile]['type'];
- $types = [
- 'image',
- 'excel',
- ];
- $filetypeuse = "*" . $filetype;
- $iscanupload = false;
- foreach ($types as $key => $value) {
- if (strpos($filetypeuse, $value)) {
- $iscanupload = true;
- }
- }
- if (!$iscanupload) {
- log::info($_FILES);
- $msg = "file type error: " . $filetype;
- log::info($msg);
- $this->error($msg);
- }
- }
- /**
- * 指定文件位置上传
- *
- * @return void
- */
- public function uploadfilebydir()
- {
- Log::info($_FILES);
- $this->filterfiletype();
- // 获取表单上传文件
- $file = request()->file('file');
- if (empty($file)) {
- $this->error('请选择上传文件');
- }
- $dir = request()->param('dir', 'appointment');
- if (!preg_match("/^[a-z]{5,30}$/", $dir)) {
- $this->error('请求错误');
- }
- $dir = 'Uploads' . '/' . $dir;
- $domainpath = getselfurl() . '/' . $dir . '/';
- // 移动到框架应用根目录/public/uploads/ 目录下
- $info = $file->move(ROOT_PATH . 'public' . '/' . $dir);
- //如果不清楚文件上传的具体键名,可以直接打印$info来查看
- //获取文件(文件名),$info->getFilename() ***********不同之处,笔记笔记哦
- //获取文件(日期/文件名),$info->getSaveName() **********不同之处,笔记笔记哦
- $filename = $info->getSaveName(); //在测试的时候也可以直接打印文件名称来查看
- $filename = str_replace(DS, "/", $filename);
- if ($filename) {
- //$this->success('文件上传成功!');
- $imageUrl = $domainpath . $filename;
- $r_upload['code'] = '200';
- $r_upload['resultData'] = $imageUrl;
- //不转义反斜杠
- return json_encode($r_upload, 320);
- } else {
- // 上传失败获取错误信息
- $this->error($file->getError());
- }
- }
- public function upQuestionsWrite()
- {
- log::info($_FILES);
- $this->filterfiletype();
- // 获取表单上传文件
- $file = request()->file('file');
- $domainpath = 'https://' . $_SERVER['HTTP_HOST'] . '/Uploads/';
- if (empty($file)) {
- $this->error('请选择上传文件');
- }
- // 移动到框架应用根目录/public/uploads/ 目录下
- $info = $file->move(ROOT_PATH . 'public' . DS . 'Uploads');
- //如果不清楚文件上传的具体键名,可以直接打印$info来查看
- //获取文件(文件名),$info->getFilename() ***********不同之处,笔记笔记哦
- //获取文件(日期/文件名),$info->getSaveName() **********不同之处,笔记笔记哦
- $filename = $info->getSaveName(); //在测试的时候也可以直接打印文件名称来查看
- if ($filename) {
- //$this->success('文件上传成功!');
- $imageUrl = $domainpath . $filename;
- $r_upload['code'] = '200';
- $r_upload['resultData'] = $imageUrl;
- //不转义反斜杠
- return json_encode($r_upload, 320);
- } else {
- // 上传失败获取错误信息
- $this->error($file->getError());
- }
- }
- }
|