Pic.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace app\admin\controller;
  3. class Pic extends AdminControl
  4. {
  5. public function upload()
  6. {
  7. if (!empty($_FILES['file']['name'])) {
  8. $pic_type_id = intval(input('param.pic_type_id'));
  9. $pic_type = input('param.pic_type');
  10. switch ($pic_type){
  11. case 'product':
  12. $pic_type_url = ATTACH_PRODUCT;
  13. break;
  14. case 'cases':
  15. $pic_type_url = ATTACH_CASES;
  16. break;
  17. case 'news':
  18. $pic_type_url = ATTACH_NEWS;
  19. break;
  20. default:
  21. break;
  22. }
  23. $upload_file = BASE_UPLOAD_PATH . DS . $pic_type_url;
  24. $upload = request()->file('file');
  25. $info = $upload->validate(['ext' => 'jpg,png,gif,jpeg'])->move($upload_file);
  26. if ($info) {
  27. $file_name = $info->getFilename();
  28. $save_name = $info->getSaveName();
  29. list($width, $height, $type, $attr) = getimagesize($upload_file . DS . $save_name);
  30. $insert = array(
  31. 'pic_type' => $pic_type,
  32. 'pic_type_id' => $pic_type_id,
  33. 'pic_name' => $file_name,
  34. 'pic_cover' => $save_name,
  35. 'pic_size' => intval($_FILES['file']['size']),
  36. 'pic_time' => TIMESTAMP,
  37. );
  38. $result = model('pic')->addpic($insert);
  39. $file_url = "get_".$pic_type."_Img";
  40. if ($result) {
  41. $data = array(
  42. 'file_id' => $result,
  43. 'file_name' => $file_name,
  44. 'file_url' => $file_url($save_name)
  45. );
  46. $output = json_encode($data);
  47. echo $output;
  48. }
  49. }
  50. }
  51. }
  52. /**
  53. * 删除图片
  54. */
  55. function del()
  56. {
  57. $pic_id = intval(input('param.file_id'));
  58. $pic_type = intval(input('param.pic_type'));
  59. if ($pic_id > 0) {
  60. $result = model('pic')->delPic(array('pic_id' => $pic_id),$pic_type);
  61. if ($result > 0) {
  62. echo 'true';
  63. exit;
  64. }
  65. }
  66. echo 'false';
  67. }
  68. }