activityimgupload.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2017/12/6 0006
  6. * Time: 17:30
  7. */
  8. header('Access-Control-Allow-Origin:*');//允许跨域
  9. $lang = $_REQUEST['lang'];
  10. $imgfile = 'imgfile_'.$lang;
  11. $file = $_FILES[$imgfile];
  12. //$fileName = $_REQUEST['fileName'];
  13. $langList = empty($langStr) ? [] : explode(',', $langStr);
  14. $imgDir = $_POST['imgDir'] ? $_POST['imgDir'] : 'images/activity/'.$lang;
  15. $imgDir = '../'.$imgDir;
  16. //如果语言不为空,则分别把图片上传到每个语言文件夹
  17. if(!empty($lang)) {
  18. die(upload($file, $imgDir));
  19. }else {
  20. die(response(4, '参数错误'));
  21. }
  22. /**
  23. * 移动上传文件到指定文件夹
  24. * @param $fileName
  25. * @param $srcFile
  26. * @param $desImgDir
  27. * @return json
  28. */
  29. function move_upload($fileName, $srcFile, $desImgDir)
  30. {
  31. $uploadPath = getFolder($desImgDir);
  32. if(!$uploadPath) {
  33. return response(1, '存放上传图片文件夹创建失败');
  34. }
  35. if(empty($srcFile['name'])) {
  36. return response(2, '上传文件为空');
  37. }
  38. $newFile = $uploadPath . '/' . $fileName;
  39. if(!move_uploaded_file($srcFile[ "tmp_name" ], $newFile)) {
  40. return response(3, '文件移动出错');
  41. }
  42. return response(0, '文件上传成功', ['imgPath' => $fileName]);
  43. }
  44. /**
  45. * 进行文件上传
  46. * @param $imgDir
  47. * @return json
  48. */
  49. function upload($file, $imgDir) {
  50. $uploadPath = getFolder($imgDir);
  51. if(!$uploadPath) {
  52. return response(1, '存放上传图片文件夹创建失败');
  53. }
  54. if(empty($file['name'])) {
  55. return response(2, '上传文件为空');
  56. }
  57. $fileName = getName($file['name']);
  58. $newFile = $uploadPath . '/' . $fileName;
  59. if(!move_uploaded_file($file[ "tmp_name" ], $newFile)) {
  60. return response(3, '文件移动出错');
  61. }
  62. return response(0, '文件上传成功', ['imgPath' => $fileName]);
  63. }
  64. /**
  65. * 进行文件上传 多语言文件夹图片上传
  66. * @param $imgDir
  67. * @return json
  68. */
  69. function uploadLangList($file, $imgDir, $langList) {
  70. if(empty($langList)) {
  71. return response(4, '上传路径错误');
  72. }
  73. $fileName = getName($file['name']);
  74. $count = 0;
  75. $tmpFilePath = '';
  76. foreach ($langList as $lang) {
  77. $tmpDir = $imgDir . $lang;
  78. $uploadPath = getFolder($tmpDir);
  79. if(!$uploadPath) {
  80. return response(1, '存放上传图片文件夹创建失败');
  81. }
  82. if(empty($file['name'])) {
  83. return response(2, '上传文件为空');
  84. }
  85. $newFile = $uploadPath . '/' . $fileName;
  86. //由于move_uploaded_file会删除之前的文件,所以后面的几张图片改为复制
  87. if($count == 0 ) {
  88. if(!move_uploaded_file($file[ "tmp_name" ], $newFile)){
  89. return response(3, '文件移动出错');
  90. }
  91. $tmpFilePath = $newFile;
  92. }
  93. if($count > 0) {
  94. if(!copy($tmpFilePath, $newFile)) {
  95. return response(5, '文件复制出错');
  96. }
  97. }
  98. $count++;
  99. }
  100. return response(0, '文件上传成功', ['imgPath' => $fileName]);
  101. }
  102. //---------end----------
  103. /**
  104. * 自动创建存储文件夹
  105. * @return string
  106. */
  107. function getFolder($pathStr)
  108. {
  109. if ( !file_exists( $pathStr ) ) {
  110. if ( !mkdir( $pathStr , 0777 , true ) ) {
  111. return false;
  112. }
  113. }
  114. return $pathStr;
  115. }
  116. /**
  117. * 重命名文件
  118. * @return string
  119. */
  120. function getName($oriName)
  121. {
  122. $ext = strtolower(strrchr($oriName, '.'));
  123. if (!$ext) {
  124. $ext = '.jpg';
  125. }
  126. return $fileName =uniqid().'_'.rand(1, 999).$ext;
  127. }
  128. /**
  129. * 统一输出返回json内容
  130. *
  131. * @access public
  132. * @date 2016-08-16
  133. * @param int $ret 状态码,0成功
  134. * @param string $msg 错误信息
  135. * @param array $data 返回数据内容
  136. * @return json;
  137. */
  138. function response($ret = 0, $msg = '', array $data = array())
  139. {
  140. $result = [
  141. 'ret' => $ret,
  142. 'msg' => $msg,
  143. 'data' => $data,
  144. ];
  145. return json_encode($result);
  146. }