Attachment.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace app\worker\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 tplFieldFile()
  11. {
  12. $file = request()->file("file");
  13. $savename = \think\facade\Filesystem::disk('public')->putFile('files',$file);
  14. if($file){
  15. $filename = str_replace(strrchr($_FILES['file']['name'], "."),"",$_FILES['file']['name']);
  16. $attachment = AttachmentModel::create([
  17. 'filename' => $filename,
  18. 'atype' => 4,
  19. 'attachment' => request()->domain() ."/attachment/". str_replace("\\","/",$savename),
  20. 'createtime' => time()
  21. ]);
  22. exit(json_encode(array(
  23. 'code' => 0,
  24. 'msg' => "",
  25. 'data' => array(
  26. 'src' => request()->domain() ."/attachment/". str_replace("\\","/",$savename),
  27. 'path' => "./attachment/". str_replace("\\","/",$savename),
  28. 'title' => $filename
  29. )
  30. )));
  31. }else{
  32. exit(json_encode(array(
  33. 'code' => 1,
  34. 'msg' => "上传失败,请稍后重试"
  35. )));
  36. }
  37. }
  38. // 图片上传
  39. public function tplFieldImage()
  40. {
  41. $file = request()->file("file");
  42. $savename = \think\facade\Filesystem::disk('public')->putFile('images',$file);
  43. if($file){
  44. $filename = str_replace(strrchr($_FILES['file']['name'], "."),"",$_FILES['file']['name']);
  45. $attachment = AttachmentModel::create([
  46. 'filename' => $filename,
  47. 'atype' => 1,
  48. 'attachment' => request()->domain() ."/attachment/". str_replace("\\","/",$savename),
  49. 'createtime' => time()
  50. ]);
  51. exit(json_encode(array(
  52. 'code' => 0,
  53. 'msg' => "",
  54. 'data' => array(
  55. 'src' => request()->domain() ."/attachment/". str_replace("\\","/",$savename),
  56. 'title' => $filename
  57. )
  58. )));
  59. }else{
  60. exit(json_encode(array(
  61. 'code' => 1,
  62. 'msg' => "上传失败,请稍后重试"
  63. )));
  64. }
  65. }
  66. }