FastAdmin 的上传代码在哪里?

FastAdmin 默认是通过 Ajax 上传到本地。

使用 admin/ajax/upload 方式返回 url。

代码文件地址在 application\admin\controller\Ajax.php 中。

  1. public function upload()
  2. {
  3. Config::set('default_return_type', 'json');
  4. $file = $this->request->file('file');
  5. if (empty($file)) {
  6. $this->error(__('No file upload or server upload limit exceeded'));
  7. }
  8. //判断是否已经存在附件
  9. $sha1 = $file->hash();
  10. $upload = Config::get('upload');
  11. preg_match('/(\d+)(\w+)/', $upload['maxsize'], $matches);
  12. $type = strtolower($matches[2]);
  13. $typeDict = ['b' => 0, 'k' => 1, 'kb' => 1, 'm' => 2, 'mb' => 2, 'gb' => 3, 'g' => 3];
  14. $size = (int)$upload['maxsize'] * pow(1024, isset($typeDict[$type]) ? $typeDict[$type] : 0);
  15. $fileInfo = $file->getInfo();
  16. $suffix = strtolower(pathinfo($fileInfo['name'], PATHINFO_EXTENSION));
  17. $suffix = $suffix ? $suffix : 'file';
  18. $mimetypeArr = explode(',', strtolower($upload['mimetype']));
  19. $typeArr = explode('/', $fileInfo['type']);
  20. //验证文件后缀
  21. if ($upload['mimetype'] !== '*' &&
  22. (
  23. !in_array($suffix, $mimetypeArr)
  24. || (stripos($typeArr[0] . '/', $upload['mimetype']) !== false && (!in_array($fileInfo['type'], $mimetypeArr) && !in_array($typeArr[0] . '/*', $mimetypeArr)))
  25. )
  26. ) {
  27. $this->error(__('Uploaded file format is limited'));
  28. }
  29. $replaceArr = [
  30. '{year}' => date("Y"),
  31. '{mon}' => date("m"),
  32. '{day}' => date("d"),
  33. '{hour}' => date("H"),
  34. '{min}' => date("i"),
  35. '{sec}' => date("s"),
  36. '{random}' => Random::alnum(16),
  37. '{random32}' => Random::alnum(32),
  38. '{filename}' => $suffix ? substr($fileInfo['name'], 0, strripos($fileInfo['name'], '.')) : $fileInfo['name'],
  39. '{suffix}' => $suffix,
  40. '{.suffix}' => $suffix ? '.' . $suffix : '',
  41. '{filemd5}' => md5_file($fileInfo['tmp_name']),
  42. ];
  43. $savekey = $upload['savekey'];
  44. $savekey = str_replace(array_keys($replaceArr), array_values($replaceArr), $savekey);
  45. $uploadDir = substr($savekey, 0, strripos($savekey, '/') + 1);
  46. $fileName = substr($savekey, strripos($savekey, '/') + 1);
  47. //
  48. $splInfo = $file->validate(['size' => $size])->move(ROOT_PATH . '/public' . $uploadDir, $fileName);
  49. if ($splInfo) {
  50. $imagewidth = $imageheight = 0;
  51. if (in_array($suffix, ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf'])) {
  52. $imgInfo = getimagesize($splInfo->getPathname());
  53. $imagewidth = isset($imgInfo[0]) ? $imgInfo[0] : $imagewidth;
  54. $imageheight = isset($imgInfo[1]) ? $imgInfo[1] : $imageheight;
  55. }
  56. $params = array(
  57. 'admin_id' => (int)$this->auth->id,
  58. 'user_id' => 0,
  59. 'filesize' => $fileInfo['size'],
  60. 'imagewidth' => $imagewidth,
  61. 'imageheight' => $imageheight,
  62. 'imagetype' => $suffix,
  63. 'imageframes' => 0,
  64. 'mimetype' => $fileInfo['type'],
  65. 'url' => $uploadDir . $splInfo->getSaveName(),
  66. 'uploadtime' => time(),
  67. 'storage' => 'local',
  68. 'sha1' => $sha1,
  69. );
  70. $attachment = model("attachment");
  71. $attachment->data(array_filter($params));
  72. $attachment->save();
  73. \think\Hook::listen("upload_after", $attachment);
  74. $this->success(__('Upload successful'), null, [
  75. 'url' => $uploadDir . $splInfo->getSaveName()
  76. ]);
  77. } else {
  78. // 上传失败获取错误信息
  79. $this->error($file->getError());
  80. }
  81. }

FastAdmin 的上传代码在哪里?的更多相关文章

  1. 【iOS 使用github上传代码】详解

    [iOS 使用github上传代码]详解 一.github创建新工程 二.直接添加文件 三.通过https 和 SSH 操作两种方式上传工程 3.1https 和 SSH 的区别: 3.1.1.前者可 ...

  2. 使用webstom或者idea上传代码到github或coding

    鉴于github网络速度太慢,建议用coding.先介绍github上传方式,因为webstom或idea集成了github,方法简单. git是一个版本控制器,他的作用是管理代码.比如你修改了代码, ...

  3. linux 下向github上传代码

    上传代码: cd TPS/devices/M8 git init                      #//初始化 git add .                    #如果是.表示上传全 ...

  4. Upload java coed in Ubuntu(在Linux 16上,上传代码)

    指令如下:(按照如下指令顺序执行即可) 1.git status  (注释:可以查看哪些代码改动了) 2.git add .     (注意:不要漏掉add后面的" . ",将本地 ...

  5. windows上传代码到github

    上传代码到github上有很多种方法,在这里我介绍一种比较简单的一种.工具嘛,越简单越好用啊. 1.首先下载github在windows下的客户端 下载地址:https://desktop.githu ...

  6. git入门学习(二):新建分支/上传代码/删除分支

    一.git新建分支,上传代码到新的不同分支  我要实现的效果,即是多个内容的平行分支:这样做的主要目的是方便统一管理属于同一个内容的不同的项目,互不干扰.如图所示: 前提是我的github上已经有we ...

  7. Github上传代码菜鸟超详细教程【转】

    最近需要将课设代码上传到Github上,之前只是用来fork别人的代码. 这篇文章写得是windows下的使用方法. 第一步:创建Github新账户 第二步:新建仓库 第三部:填写名称,简介(可选), ...

  8. SVN上传代码

    概述 SVN上传代码 使用SVN工具上传代码到新浪SAE 1.下载SVN工具http://tortoisesvn.net/downloads.html 2.创建一个文件夹,Checkout,输入SAE ...

  9. iOS如何上传代码到Github

    iOS如何上传代码到Github 很多iOS开发者想开源自己的代码或者demo,开源到Github是个不错的选择,那么如何上传我们的代码到Github,令所有人可以下载使用呢?这里我们的目的很明确,就 ...

随机推荐

  1. Linux - 包不同安装方式

    rpm 软件包管理器 安装编译包好的二进制包 方式 rpm -ivh lynx # rpm安装 rpm -e lynx # 卸载包 rpm -e lynx --nodeps # 强制卸载 rpm -q ...

  2. NOIP2018ty记

    前置传送门:noip2018前流水账 Day-inf~Day-3 写流水账里了 懒得再写了 Day-2~Day-1 做了些noip的原题 真是奇怪,我天天爱跑步和逛公园都是1A的,结果反而有些普及组的 ...

  3. CSS —— 选择器

    选择器种类 标签选择器 id选择器 类选择器 通配符 交集选择器 并集选择器 后代选择器 子代选择器 选择器设置样式优先级 默认样式 < 继承样式 < 通配符设置样式 < 标签选择器 ...

  4. react框架实现点击事件计数小案例

    下面将以一个小案例来讲解react的框架的一般应用,重点内容在代码段都有详细的解释,希望对大家有帮助 代码块: 代码块: import React from 'react'; import React ...

  5. DjangoAdmin自定义过滤器

    class UserIDFilter(admin.SimpleListFilter): # 自定义用户查询过滤器 title = _('关联用户') parameter_name = 'user_id ...

  6. snmp 发送类型ASN_OBJECT_ID

    参考链接: http://blog.csdn.net/yin138/article/details/50388878 ,,,,,,,,,}; int ret = snmp_set_var_typed_ ...

  7. C++ vector 使用笔记

    map 插入 vector #include <string> #include <iostream> #include <algorithm> #include ...

  8. ROS 时间同步问题

    0. 问题 两台ubuntu主机无法与一台debian主机使用分布式通信,摄像头发出的话题机器人收不到,考虑是时间同步的问题. 也可能是系统不统一的问题; 今天在家实验了一下,时间差6min,照样可以 ...

  9. K-means聚类算法原理和C++实现

    给定训练集$\{x^{(1)},...,x^{(m)}\}$,想把这些样本分成不同的子集,即聚类,$x^{(i)}\in\mathbb{R^{n}}$,但是这是个无标签数据集,也就是说我们再聚类的时候 ...

  10. mysql服务里面没有启动项

    解决:5.0版本:开始->运行->cmd,进到mysql安装的bin目录D:\MySQL\bin>mysqld.exe -installService successfully in ...