Attachment.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace app\mainapp\controller;
  3. use think\facade\Session;
  4. use think\facade\Request;
  5. use think\facade\Filesystem;
  6. use app\common\model\Attachment as AttachmentModel;
  7. class Attachment
  8. {
  9. // 单个图片上传
  10. public function tplFieldImage()
  11. {
  12. $this->validateExt(['jpg', 'png', 'jpeg']);
  13. $file = request()->file("file");
  14. $savename = \think\facade\Filesystem::disk('public')->putFile('images', $file);
  15. if ($file) {
  16. $filename = str_replace(strrchr($_FILES['file']['name'], "."), "", $_FILES['file']['name']);
  17. $attachment = AttachmentModel::create([
  18. 'filename' => $filename,
  19. 'atype' => 1,
  20. 'attachment' => str_replace("\\", "/", $savename),
  21. 'createtime' => time(),
  22. ]);
  23. page_result(0, "", [
  24. 'src' => request()->domain() . "/attachment/" . str_replace("\\", "/", $savename),
  25. 'path' => "/attachment/" . str_replace("\\", "/", $savename),
  26. 'title' => $filename,
  27. ]);
  28. } else {
  29. page_result(1, "上传失败,请稍后重试");
  30. }
  31. }
  32. // 视频文件上传
  33. public function tplFieldVideo()
  34. {
  35. $this->validateExt(['mp4', 'avi', 'wmv', 'mpg', 'mpeg']);
  36. $file = request()->file("file");
  37. $savename = \think\facade\Filesystem::disk('public')->putFile('videos', $file);
  38. if ($file) {
  39. $filename = str_replace(strrchr($_FILES['file']['name'], "."), "", $_FILES['file']['name']);
  40. $attachment = AttachmentModel::create([
  41. 'filename' => $filename,
  42. 'atype' => 3,
  43. 'attachment' => str_replace("\\", "/", $savename),
  44. 'createtime' => time(),
  45. ]);
  46. page_result(0, "", [
  47. 'src' => request()->domain() . "/attachment/" . str_replace("\\", "/", $savename),
  48. 'path' => "/attachment/" . str_replace("\\", "/", $savename),
  49. 'title' => $filename,
  50. ]);
  51. } else {
  52. page_result(1, "上传失败,请稍后重试");
  53. }
  54. }
  55. public function validateExt($ext_arr)
  56. {
  57. $ext = pathinfo($_FILES['file']['name'])['extension'];
  58. if (!in_array($ext, $ext_arr)) {
  59. page_result(1, "文件格式不正确");
  60. }
  61. }
  62. }