You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
7.2 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Api\CommonController;
  4. use Illuminate\Http\Request;
  5. use App\Common\ReturnData;
  6. use App\Common\Helper;
  7. class ImageController extends CommonController
  8. {
  9. public $path;
  10. public function __construct()
  11. {
  12. parent::__construct();
  13. $this->path = '/uploads/'.date('Y/m',time());
  14. }
  15. //单文件/图片上传,成功返回路径,不含域名
  16. public function imageUpload(Request $request)
  17. {
  18. $file = $_FILES['file'];//得到传输的数据
  19. $type = strtolower(substr(strrchr($file["name"], '.'), 1)); //文件后缀
  20. $image_path = $this->path.'/'.date('Ymdhis',time()).rand(1000,9999).'.'.$type;
  21. $uploads_path = $this->path; //存储路径
  22. $allow_type = array('jpg','jpeg','gif','png','doc','docx','txt','pdf'); //定义允许上传的类型
  23. //判断文件类型是否被允许上传
  24. if(!in_array($type, $allow_type))
  25. {
  26. //如果不被允许,则直接停止程序运行
  27. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'文件格式不正确');
  28. }
  29. //判断是否是通过HTTP POST上传的
  30. if(!is_uploaded_file($file['tmp_name']))
  31. {
  32. //如果不是通过HTTP POST上传的
  33. return ReturnData::create(ReturnData::SYSTEM_FAIL);
  34. }
  35. //文件小于1M
  36. if ($file["size"] < 2048000)
  37. {
  38. if ($file["error"] > 0)
  39. {
  40. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,$file["error"]);
  41. }
  42. else
  43. {
  44. if(!file_exists(base_path('public').$uploads_path))
  45. {
  46. Helper::createDir(base_path('public').$uploads_path); //创建文件夹;
  47. }
  48. move_uploaded_file($file["tmp_name"], base_path('public').$image_path);
  49. }
  50. }
  51. else
  52. {
  53. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'文件不得超过2M');
  54. }
  55. return ReturnData::create(ReturnData::SUCCESS,$image_path);
  56. }
  57. /**
  58. * 多文件上传,成功返回路径,不含域名
  59. * 多文件上传格式:
  60. * <input type="file" name="file[]">
  61. * <input type="file" name="file[]">
  62. * <input type="file" name="file[]">
  63. */
  64. public function multipleImageUpload(Request $request)
  65. {
  66. $res = [];
  67. $file = $_FILES['file'];//得到传输的数据
  68. if($file)
  69. {
  70. foreach($file['name'] as $key=>$value)
  71. {
  72. $type = strtolower(substr(strrchr($file["name"][$key], '.'), 1)); //文件后缀
  73. $image_path = $this->path.'/'.date('Ymdhis',time()).rand(1000,9999).'.'.$type;
  74. $uploads_path = $this->path; //存储路径
  75. $allow_type = array('jpg','jpeg','gif','png','doc','docx','txt','pdf'); //定义允许上传的类型
  76. //判断文件类型是否被允许上传
  77. if(!in_array($type, $allow_type))
  78. {
  79. //如果不被允许,则直接停止程序运行
  80. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'文件格式不正确');
  81. }
  82. //判断是否是通过HTTP POST上传的
  83. if(!is_uploaded_file($file['tmp_name'][$key]))
  84. {
  85. //如果不是通过HTTP POST上传的
  86. return ReturnData::create(ReturnData::SYSTEM_FAIL);
  87. }
  88. //文件小于2M
  89. if ($file["size"][$key] < 2048000)
  90. {
  91. if ($file["error"][$key] > 0)
  92. {
  93. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,$file["error"][$key]);
  94. }
  95. else
  96. {
  97. if(!file_exists(base_path('public').$uploads_path))
  98. {
  99. Helper::createDir(base_path('public').$uploads_path); //创建文件夹;
  100. }
  101. move_uploaded_file($file["tmp_name"][$key], base_path('public').$image_path);
  102. }
  103. }
  104. else
  105. {
  106. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'文件不得超过2M');
  107. }
  108. $res[] = $image_path;
  109. }
  110. }
  111. return ReturnData::create(ReturnData::SUCCESS,$res);
  112. }
  113. /**
  114. * 多文件上传,成功返回路径,不含域名
  115. * 多文件上传格式:
  116. * <input type="file" name="file1">
  117. * <input type="file" name="file2">
  118. * <input type="file" name="file3">
  119. */
  120. public function multipleFileUpload(Request $request)
  121. {
  122. $res = [];
  123. $files = $_FILES;//得到传输的数据
  124. if($files)
  125. {
  126. foreach($files as $key=>$file)
  127. {
  128. $type = strtolower(substr(strrchr($file["name"], '.'), 1)); //文件后缀
  129. $image_path = $this->path.'/'.date('Ymdhis',time()).rand(1000,9999).'.'.$type;
  130. $uploads_path = $this->path; //存储路径
  131. $allow_type = array('jpg','jpeg','gif','png','doc','docx','txt','pdf'); //定义允许上传的类型
  132. //判断文件类型是否被允许上传
  133. if(!in_array($type, $allow_type))
  134. {
  135. //如果不被允许,则直接停止程序运行
  136. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'文件格式不正确');
  137. }
  138. //判断是否是通过HTTP POST上传的
  139. if(!is_uploaded_file($file['tmp_name']))
  140. {
  141. //如果不是通过HTTP POST上传的
  142. return ReturnData::create(ReturnData::SYSTEM_FAIL);
  143. }
  144. //文件小于1M
  145. if ($file["size"] < 2048000)
  146. {
  147. if ($file["error"] > 0)
  148. {
  149. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,$file["error"]);
  150. }
  151. else
  152. {
  153. if(!file_exists(base_path('public').$uploads_path))
  154. {
  155. Helper::createDir(base_path('public').$uploads_path); //创建文件夹;
  156. }
  157. move_uploaded_file($file["tmp_name"], base_path('public').$image_path);
  158. }
  159. }
  160. else
  161. {
  162. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'文件不得超过2M');
  163. }
  164. $res[] = $image_path;
  165. }
  166. }
  167. return ReturnData::create(ReturnData::SUCCESS,$res);
  168. }
  169. }