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.

319 lines
11 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
7 years ago
8 years ago
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use Illuminate\Http\Request;
  4. use App\Common\ReturnData;
  5. use App\Common\Helper;
  6. class ImageController extends CommonController
  7. {
  8. public $path;
  9. public function __construct()
  10. {
  11. parent::__construct();
  12. $this->path = '/uploads/'.date('Y/m',time());
  13. }
  14. //单文件/图片上传,成功返回路径,不含域名
  15. public function imageUpload(Request $request)
  16. {
  17. $file = $_FILES['file'];//得到传输的数据
  18. $type = strtolower(substr(strrchr($file["name"], '.'), 1)); //文件后缀
  19. $image_path = $this->path.'/'.date('Ymdhis',time()).rand(1000,9999).'.'.$type;
  20. $uploads_path = $this->path; //存储路径
  21. $allow_type = array('jpg','jpeg','gif','png','doc','docx','txt','pdf'); //定义允许上传的类型
  22. //判断文件类型是否被允许上传
  23. if(!in_array($type, $allow_type))
  24. {
  25. //如果不被允许,则直接停止程序运行
  26. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'文件格式不正确');
  27. }
  28. //判断是否是通过HTTP POST上传的
  29. if(!is_uploaded_file($file['tmp_name']))
  30. {
  31. //如果不是通过HTTP POST上传的
  32. return ReturnData::create(ReturnData::SYSTEM_FAIL);
  33. }
  34. //文件小于1M
  35. if ($file["size"] < 2048000)
  36. {
  37. if ($file["error"] > 0)
  38. {
  39. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,$file["error"]);
  40. }
  41. else
  42. {
  43. if(!file_exists(base_path('public').$uploads_path))
  44. {
  45. Helper::createDir(base_path('public').$uploads_path); //创建文件夹;
  46. }
  47. move_uploaded_file($file["tmp_name"], base_path('public').$image_path);
  48. }
  49. }
  50. else
  51. {
  52. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'文件不得超过2M');
  53. }
  54. return ReturnData::create(ReturnData::SUCCESS,$image_path);
  55. }
  56. //阿里云OSS图片上传
  57. public function ossImageUpload()
  58. {
  59. $res = $this->aliyunOSSFileUpload($_FILES);
  60. if($res['code'] == 1)
  61. {
  62. $this->success($res['data']);
  63. }
  64. $this->error($res['msg']);
  65. }
  66. /**
  67. * 多文件上传,成功返回路径,不含域名
  68. * 多文件上传格式:
  69. * <input type="file" name="file[]">
  70. * <input type="file" name="file[]">
  71. * <input type="file" name="file[]">
  72. */
  73. public function multipleImageUpload(Request $request)
  74. {
  75. $res = [];
  76. $file = $_FILES['file'];//得到传输的数据
  77. if($file)
  78. {
  79. foreach($file['name'] as $key=>$value)
  80. {
  81. $type = strtolower(substr(strrchr($file["name"][$key], '.'), 1)); //文件后缀
  82. $image_path = $this->path.'/'.date('Ymdhis',time()).rand(1000,9999).'.'.$type;
  83. $uploads_path = $this->path; //存储路径
  84. $allow_type = array('jpg','jpeg','gif','png','doc','docx','txt','pdf'); //定义允许上传的类型
  85. //判断文件类型是否被允许上传
  86. if(!in_array($type, $allow_type))
  87. {
  88. //如果不被允许,则直接停止程序运行
  89. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'文件格式不正确');
  90. }
  91. //判断是否是通过HTTP POST上传的
  92. if(!is_uploaded_file($file['tmp_name'][$key]))
  93. {
  94. //如果不是通过HTTP POST上传的
  95. return ReturnData::create(ReturnData::SYSTEM_FAIL);
  96. }
  97. //文件小于2M
  98. if ($file["size"][$key] < 2048000)
  99. {
  100. if ($file["error"][$key] > 0)
  101. {
  102. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,$file["error"][$key]);
  103. }
  104. else
  105. {
  106. if(!file_exists(base_path('public').$uploads_path))
  107. {
  108. Helper::createDir(base_path('public').$uploads_path); //创建文件夹;
  109. }
  110. move_uploaded_file($file["tmp_name"][$key], base_path('public').$image_path);
  111. }
  112. }
  113. else
  114. {
  115. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'文件不得超过2M');
  116. }
  117. $res[] = $image_path;
  118. }
  119. }
  120. return ReturnData::create(ReturnData::SUCCESS,$res);
  121. }
  122. /**
  123. * 多文件上传,成功返回路径,不含域名
  124. * 多文件上传格式:
  125. * <input type="file" name="file1">
  126. * <input type="file" name="file2">
  127. * <input type="file" name="file3">
  128. */
  129. public function multipleFileUpload(Request $request)
  130. {
  131. $res = [];
  132. $files = $_FILES;//得到传输的数据
  133. if($files)
  134. {
  135. foreach($files as $key=>$file)
  136. {
  137. $type = strtolower(substr(strrchr($file["name"], '.'), 1)); //文件后缀
  138. $image_path = $this->path.'/'.date('Ymdhis',time()).rand(1000,9999).'.'.$type;
  139. $uploads_path = $this->path; //存储路径
  140. $allow_type = array('jpg','jpeg','gif','png','doc','docx','txt','pdf'); //定义允许上传的类型
  141. //判断文件类型是否被允许上传
  142. if(!in_array($type, $allow_type))
  143. {
  144. //如果不被允许,则直接停止程序运行
  145. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'文件格式不正确');
  146. }
  147. //判断是否是通过HTTP POST上传的
  148. if(!is_uploaded_file($file['tmp_name']))
  149. {
  150. //如果不是通过HTTP POST上传的
  151. return ReturnData::create(ReturnData::SYSTEM_FAIL);
  152. }
  153. //文件小于1M
  154. if ($file["size"] < 2048000)
  155. {
  156. if ($file["error"] > 0)
  157. {
  158. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,$file["error"]);
  159. }
  160. else
  161. {
  162. if(!file_exists(base_path('public').$uploads_path))
  163. {
  164. Helper::createDir(base_path('public').$uploads_path); //创建文件夹;
  165. }
  166. move_uploaded_file($file["tmp_name"], base_path('public').$image_path);
  167. }
  168. }
  169. else
  170. {
  171. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'文件不得超过2M');
  172. }
  173. $res[] = $image_path;
  174. }
  175. }
  176. return ReturnData::create(ReturnData::SUCCESS,$res);
  177. }
  178. public function aliyunOSSFileUpload($files)
  179. {
  180. $res = [];
  181. //$files = $_FILES;//得到传输的数据
  182. $path = 'data/uploads/'.date('Y/m',time());
  183. if($files)
  184. {
  185. // 对上传文件数组信息处理
  186. $files = $this->dealFiles($files);
  187. foreach($files as $key=>$file)
  188. {
  189. $type = strtolower(substr(strrchr($file["name"], '.'), 1)); //文件后缀
  190. $image_path = $path.'/'.date('Ymdhis',time()).rand(1000,9999).'.'.$type;
  191. $uploads_path = $path; //存储路径
  192. $allow_type = array('jpg','jpeg','gif','png','doc','docx','txt','pdf'); //定义允许上传的类型
  193. //判断文件类型是否被允许上传
  194. if(!in_array($type, $allow_type))
  195. {
  196. //如果不被允许,则直接停止程序运行
  197. //$this->error('文件格式不正确');
  198. return ['code'=>0,'msg'=>'文件格式不正确','data'=>''];
  199. }
  200. //判断是否是通过HTTP POST上传的
  201. if(!is_uploaded_file($file['tmp_name']))
  202. {
  203. //如果不是通过HTTP POST上传的
  204. //$this->error('上传失败');
  205. return ['code'=>0,'msg'=>'上传失败','data'=>''];
  206. }
  207. //文件小于1M
  208. if ($file["size"] < 2048000)
  209. {
  210. if ($file["error"] > 0)
  211. {
  212. //$this->error($file["error"]);
  213. return ['code'=>0,'msg'=>$file["error"],'data'=>''];
  214. }
  215. else
  216. {
  217. /* if(!file_exists(substr(ROOT_PATH, 0, -1).$uploads_path))
  218. {
  219. Helper::createDir(substr(ROOT_PATH, 0, -1).$uploads_path); //创建文件夹;
  220. }
  221. move_uploaded_file($file["tmp_name"], substr(ROOT_PATH, 0, -1).$image_path); */
  222. $image = AliyunOSS::uploadFile($image_path, $file['tmp_name']);
  223. if($image && $image['code']==1){}else{/* $this->error('系统错误'); */return ['code'=>0,'msg'=>'系统错误','data'=>''];}
  224. }
  225. }
  226. else
  227. {
  228. //$this->error('文件不得超过2M');
  229. return ['code'=>0,'msg'=>'文件不得超过2M','data'=>''];
  230. }
  231. $res[$key] = $image['data']['oss-request-url'];
  232. }
  233. }
  234. else
  235. {
  236. //$this->error('参数错误');
  237. return ['code'=>0,'msg'=>'参数错误','data'=>''];
  238. }
  239. return ['code'=>1,'msg'=>'操作成功','data'=>$res];
  240. }
  241. /**
  242. * 转换上传文件数组变量为正确的方式
  243. * @access public
  244. * @param array $files 上传的文件变量
  245. * @return array
  246. */
  247. public function dealFiles($files)
  248. {
  249. $fileArray = [];
  250. $n = 0;
  251. foreach ($files as $key => $file) {
  252. if (is_array($file['name'])) {
  253. $keys = array_keys($file);
  254. $count = count($file['name']);
  255. for ($i = 0; $i < $count; $i++) {
  256. $fileArray[$n]['key'] = $key;
  257. foreach ($keys as $_key) {
  258. $fileArray[$n][$_key] = $file[$_key][$i];
  259. }
  260. $n++;
  261. }
  262. } else {
  263. $fileArray = $files;
  264. break;
  265. }
  266. }
  267. return $fileArray;
  268. }
  269. }