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.

65 lines
2.1 KiB

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 function __construct()
  10. {
  11. parent::__construct();
  12. }
  13. //文件上传,成功返回路径,不含域名
  14. public function imageUpload(Request $request)
  15. {
  16. $file = $_FILES['file'];//得到传输的数据
  17. $type = strtolower(substr(strrchr($file["name"], '.'), 1)); //文件后缀
  18. $image_path = '/uploads/'.date('Y/m',time()).'/'.date('Ymdhis',time()).rand(1000,9999).'.'.$type;
  19. $uploads_path = '/uploads/'.date('Y/m',time()); //存储路径
  20. $allow_type = array('jpg','jpeg','gif','png','doc','docx','txt'); //定义允许上传的类型
  21. //判断文件类型是否被允许上传
  22. if(!in_array($type, $allow_type))
  23. {
  24. //如果不被允许,则直接停止程序运行
  25. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'文件格式不正确');
  26. }
  27. //判断是否是通过HTTP POST上传的
  28. if(!is_uploaded_file($file['tmp_name']))
  29. {
  30. //如果不是通过HTTP POST上传的
  31. return ReturnData::create(ReturnData::SYSTEM_FAIL);
  32. }
  33. //文件小于1M
  34. if ($file["size"] < 1024000)
  35. {
  36. if ($file["error"] > 0)
  37. {
  38. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,$file["error"]);
  39. }
  40. else
  41. {
  42. if(!file_exists(base_path('public').$uploads_path))
  43. {
  44. Helper::createDir(base_path('public').$uploads_path); //创建文件夹;
  45. }
  46. move_uploaded_file($file["tmp_name"], base_path('public').$image_path);
  47. }
  48. }
  49. else
  50. {
  51. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'文件不得超过1M');
  52. }
  53. return ReturnData::create(ReturnData::SUCCESS,$image_path);
  54. }
  55. }