Attachment.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. $file = request()->file("file");
  13. $savename = \think\facade\Filesystem::disk('public')->putFile('images',$file);
  14. if($file){
  15. $filename = str_replace(strrchr($_FILES['file']['name'], "."),"",$_FILES['file']['name']);
  16. $attachment = AttachmentModel::create([
  17. 'filename' => $filename,
  18. 'atype' => 1,
  19. 'attachment' => str_replace("\\","/",$savename),
  20. 'createtime' => time()
  21. ]);
  22. page_result(0, "", array(
  23. 'src' => request()->domain() ."/attachment/". str_replace("\\","/",$savename),
  24. 'path' => "/attachment/". str_replace("\\","/",$savename),
  25. 'title' => $filename
  26. ));
  27. }else{
  28. page_result(1, "上传失败,请稍后重试");
  29. }
  30. }
  31. // 视频文件上传
  32. public function tplFieldVideo()
  33. {
  34. $file = request()->file("file");
  35. $savename = \think\facade\Filesystem::disk('public')->putFile('videos',$file);
  36. if($file){
  37. $filename = str_replace(strrchr($_FILES['file']['name'], "."),"",$_FILES['file']['name']);
  38. $attachment = AttachmentModel::create([
  39. 'filename' => $filename,
  40. 'atype' => 3,
  41. 'attachment' => str_replace("\\","/",$savename),
  42. 'createtime' => time()
  43. ]);
  44. page_result(0, "", array(
  45. 'src' => request()->domain() ."/attachment/". str_replace("\\","/",$savename),
  46. 'path' => "/attachment/". str_replace("\\","/",$savename),
  47. 'title' => $filename
  48. ));
  49. }else{
  50. page_result(1, "上传失败,请稍后重试");
  51. }
  52. }
  53. }