12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- <?php
- namespace app\mainapp\controller;
- use think\facade\Session;
- use think\facade\Request;
- use think\facade\Filesystem;
- use app\common\model\Attachment as AttachmentModel;
- class Attachment
- {
- // 单个图片上传
- public function tplFieldImage()
- {
- $this->validateExt(['jpg', 'png', 'jpeg']);
- $file = request()->file("file");
- $savename = \think\facade\Filesystem::disk('public')->putFile('images', $file);
- if ($file) {
- $filename = str_replace(strrchr($_FILES['file']['name'], "."), "", $_FILES['file']['name']);
- $attachment = AttachmentModel::create([
- 'filename' => $filename,
- 'atype' => 1,
- 'attachment' => str_replace("\\", "/", $savename),
- 'createtime' => time(),
- ]);
- page_result(0, "", [
- 'src' => request()->domain() . "/attachment/" . str_replace("\\", "/", $savename),
- 'path' => "/attachment/" . str_replace("\\", "/", $savename),
- 'title' => $filename,
- ]);
- } else {
- page_result(1, "上传失败,请稍后重试");
- }
- }
- // 视频文件上传
- public function tplFieldVideo()
- {
- $this->validateExt(['mp4', 'avi', 'wmv', 'mpg', 'mpeg']);
- $file = request()->file("file");
- $savename = \think\facade\Filesystem::disk('public')->putFile('videos', $file);
- if ($file) {
- $filename = str_replace(strrchr($_FILES['file']['name'], "."), "", $_FILES['file']['name']);
- $attachment = AttachmentModel::create([
- 'filename' => $filename,
- 'atype' => 3,
- 'attachment' => str_replace("\\", "/", $savename),
- 'createtime' => time(),
- ]);
- page_result(0, "", [
- 'src' => request()->domain() . "/attachment/" . str_replace("\\", "/", $savename),
- 'path' => "/attachment/" . str_replace("\\", "/", $savename),
- 'title' => $filename,
- ]);
- } else {
- page_result(1, "上传失败,请稍后重试");
- }
- }
- public function validateExt($ext_arr)
- {
- $ext = pathinfo($_FILES['file']['name'])['extension'];
- if (!in_array($ext, $ext_arr)) {
- page_result(1, "文件格式不正确");
- }
- }
- }
|