SetBasics.class.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. <?php
  2. namespace Cas\Controller\Admin\Activity;
  3. use Cas\Module\Lottery;
  4. use Cas\Dao\PlatformBulletin;
  5. use KIF\Core\Request;
  6. use Cas\Dao\LotteryEvents;
  7. use Cas\Controller\Admin\Controller;
  8. use Cas\Dao\Platform;
  9. use Cas\Dao\Category;
  10. use KIF\Core\Config;
  11. use Cas\Dao\BackUser as DBackUser;
  12. /**
  13. * ["活动相关" - "基础设置" ]
  14. * 页面: 创建活动基础设置 doPageCreate
  15. * 事件:操作: 创建活动基础设置 doReqCreate
  16. * 页面: 修改&查看 活动基础设置 doPageShow
  17. * 事件: 修改活动基础设置 doReqUp
  18. *
  19. * @author lihuanchun 
  20. *  
  21. */
  22. class SetBasics extends Controller {
  23. private $objLottery; // 活动后端
  24. private $operatorData;
  25. /**
  26. *  初始化
  27. */
  28. public function __construct() {
  29. header ( "Content-Type: text/html; charset=utf-8" );
  30. $this->objLottery = new Lottery ();
  31. $this->operatorData = $this->getUser ();
  32. }
  33. public function doDefault() {
  34. }
  35. /**
  36. * 页面: 创建活动 基础设置
  37. * 地址:http://cas.lishuy.com/?c=Admin_Activity_SetBasics&a=PageCreate&type=nnn&bigType=ssss
  38. * 参数:
  39. * type = 活动类型ID [可空]
  40. * bigType = 活动大类字符串
  41. */
  42. public function doPageCreate() {
  43. $thisType = Request::g ( 'type' ); // 获取类型
  44. $thisBigType = Request::g ( 'bigType' ); // 获取大类型
  45. // 获取所有活动类型
  46. $typeData = LotteryEvents::getType ();
  47. // 获取活动大类配置
  48. $bigTypeConfig = LotteryEvents::getBigTypeConfig ();
  49. // 获取所有发布列表(平台列表)
  50. $platformList = array ();
  51. $objDPlatform = new Platform ();
  52. $platformList = $objDPlatform->getsAll ();
  53. if (!self::isSuperadmin()) {
  54. $objDBackUser = new DBackUser();
  55. $user = $objDBackUser->get(self::getUid());
  56. $platfrom = $user['platfrom'];
  57. $tmpPlatformList = array();
  58. foreach ($platfrom as $tmpPlatformId) {
  59. $tmpPlatformList[] = $platformList[$tmpPlatformId];
  60. }
  61. $platformList = $tmpPlatformList;
  62. }
  63. // 获取是否授权配置参数配置
  64. $authorize = LotteryEvents::getAuthorize ();
  65. // 获取是是否在列表中显示配置
  66. $listDisplay = LotteryEvents::getListDisplay ();
  67. // 是否推荐到焦点图
  68. $focusDisplay = LotteryEvents::getFocusDisplay ();
  69. // 是否置顶
  70. $topDisplay = LotteryEvents::getTopDisplay ();
  71. $objDCategory = new Category ();
  72. $categoryList = $objDCategory->getsAll ();
  73. // 音乐图标位置
  74. $getMusicPosition = LotteryEvents::getMusicPosition ();
  75. // 导航
  76. $navConfig = $this->getCreatePageNav($thisType,$thisBigType);
  77. // 微信相关模块 不包含类型
  78. $weixinFormNotType = array(
  79. LotteryEvents::TYPE_EVENTS_OTHER,
  80. );
  81. $this->setOutput ( 'weixinFormNotType', $weixinFormNotType );
  82. // 音乐相关 不包含类型
  83. $musicFormNotType = array(
  84. LotteryEvents::TYPE_EVENTS_CUT_SCREEN,
  85. );
  86. $this->setOutput ( 'musicFormNotType', $musicFormNotType );
  87. // 切屏方向 不包含的类型
  88. $cutDirectionFormNotType = array(
  89. LotteryEvents::TYPE_EVENTS_CUT_SCREEN,
  90. );
  91. $this->setOutput ( 'cutDirectionFormNotType', $cutDirectionFormNotType );
  92. $this->tpl = 'admin/activity/page_create';
  93. $title = '基础设置';
  94. $this->setOutput('title', $title);
  95. $this->setOutput('menu_active', array('name' => 'quickpublish', 'item' => '')); //激活菜单
  96. $this->addNavMenu('活动列表');
  97. $this->addNavMenu($title);
  98. $this->setOutput ( 'navConfig', $navConfig );// 导航 显示 URL 配置
  99. $this->setOutput ( 'getMusicPosition', $getMusicPosition );
  100. $this->setOutput ( 'platformList', $platformList );
  101. $this->setOutput ( 'categoryList', $categoryList );// 获取所有发布列表(平台列表)
  102. $this->setOutput ( 'focusDisplay', $focusDisplay );
  103. $this->setOutput ( 'topDisplay', $topDisplay );
  104. $this->setOutput ( 'authorize', $authorize );
  105. $this->setOutput ( 'listDisplay', $listDisplay );
  106. $this->setOutput ( 'getMusicPosition', $getMusicPosition );
  107. $this->setOutput ( 'thisBigType', $thisBigType ); // 当前选择大类
  108. $this->setOutput ( 'thisType', $thisType ); // 当前选择类型
  109. $this->setOutput ( 'typeData', $typeData ); // 所有活动类型
  110. $this->setOutput ( 'bigTypeConfig', $bigTypeConfig ); // 所有活动类型
  111. $this->setOutput('typeDesc', $typeData[$thisType]['desc']);
  112. $this->setOutput('pagePublicData', $this->getPagePublicData()); // 后台管理相关数据
  113. }
  114. /**
  115. * 事件: 创建活动 基础设置
  116. * 请求地址:http://cas.lishuy.com/?c=Admin_Activity_SetBasics&a=ReqCreate
  117. * 参数:见方法内 $info
  118. */
  119. public function doReqCreate() {
  120. $info ['type'] = Request::p ( 'type' ); // 获取所选类型
  121. $info ['img_url'] = Request::p('img_url'); // 活动头图 img_url 表单名称 img_url
  122. $info ['events_name'] = Request::p ( 'events_name' ); // 活动标题 events_name 时间戳
  123. if ($info['type'] == LotteryEvents::TYPE_EVENTS_ARTICLE) {
  124. $info ['begin_time'] = date('Y-m-d H:i');
  125. } else {
  126. $info ['begin_time'] = Request::p ( 'begin_time' ) ? Request::p ( 'begin_time' ) : '2025-01.01 00:00:00'; // 活动开始时间 begin_time 时间戳
  127. }
  128. $info ['begin_time'] = strtotime($info ['begin_time']);
  129. $info ['end_time'] = Request::p ( 'end_time' ) ? Request::p ( 'end_time' ) : '2025-01.01 00:00:00'; // 活动结束时间 end_time
  130. $info ['end_time'] = strtotime($info ['end_time']);
  131. $tmp_platform_ids = $_POST['platform_ids']; // 所属"列表"IDs platform_ids_str
  132. foreach ( $tmp_platform_ids as $platform_id ) {
  133. $info ['platform_ids'] += $platform_id;
  134. }
  135. $info ['weixinShare'] ['imgUrl'] = Request::p('wx_img_url'); // 分享图片 weixinShare[imgUrl] 表单名称 wx_img_url
  136. $info ['weixinShare'] ['title'] = Request::p ( 'weixinShare_title' ); // 分享标题 weixinShare[title]
  137. $info ['weixinShare'] ['content'] = Request::p ( 'weixinShare_content' ); // 分享内容 weixinShare[content]
  138. $info ['weixinShare'] ['jump_url'] = Request::p ( 'weixinShare_jump_url' ); // 分享后跳转地址 weixinShare[jump_url]
  139. $info ['authorize'] = Request::p ( 'authorize' ); // 是否需要登录 authorize
  140. $info ['list_display'] = Request::p ( 'list_display' ); // 是否在列表中出现
  141. $info ['participation_user_num'] = Request::p ( 'participation_user_num' ); // 每天用户参与次数 participation_user_num
  142. $info ['participation_ip_num'] = Request::p ( 'participation_ip_num' ); // 允许每天IP参与次数 participation_ip_num
  143. $info ['participation_user_all_num'] = Request::p ( 'participation_user_all_num' ); // 允许用户参与次数 participation_user_all_num
  144. $info ['winning_user_num'] = Request::p ( 'winning_user_num' ); // 允许用户获奖次数 winning_user_num
  145. $info['cut_fx'] = Request::p('cut_fx'); // 切屏方向
  146. $info ['music_url'] = Request::p('music_url'); // 获取背景音乐 music_url 表单名称:music_url
  147. $info ['operator_uid'] = $this->operatorData['uid'];// 创建人UID
  148. $info ['other_url'] = Request::p ( 'other_url' ); // 跳转地址
  149. $info ['music_position'] = Request::p ( 'music_position' ); // 音乐图标位置 music_position
  150. $info['loading_bg_color'] = Request::p('loading_bg_color');
  151. $info['loading_text_color'] = Request::p('loading_text_color');
  152. $info ['setPageType'] = 'Basics';
  153. // 执行添加方法
  154. $events_id = $this->objLottery->addLotteryEvents ( $info );
  155. if (! $events_id) {
  156. $this->ajax_fail_exit('基础设置失败');
  157. }
  158. $objPlatform = new Platform();
  159. $formData = $objPlatform -> getsAll();
  160. $eventData = $this->objLottery->getOneLotteryEvents($events_id);
  161. foreach ( $tmp_platform_ids as $platform_id ) {
  162. $url = $this->objLottery ->getEventsUrl($info ['type'], $eventData['create_time'],$formData[$platform_id]['url']);
  163. $this->createImgCodeUrl($url);
  164. }
  165. // 更具不同活动 大类型 跳转 到不同的地址
  166. $bigTypeConfig = LotteryEvents::getBigTypeConfig ();
  167. if(in_array($info['type'], $bigTypeConfig['Event'])){
  168. $redirect_url = Request::schemeDomain () . "/?c=Admin_Activity_SetInteractive&a=PageShow&events_id={$events_id}";
  169. }else{
  170. $redirect_url = Request::schemeDomain () . "/?c=Admin_Activity_SetPage&a=PageShow&events_id={$events_id}";
  171. }
  172. if($info['type'] == LotteryEvents::TYPE_EVENTS_OTHER){
  173. $redirect_url = Request::schemeDomain () . "/?c=Admin_Activity_DeliveryChannels&a=PageList&events_id={$events_id}";
  174. }
  175. $this->ajax_success_exit($redirect_url);
  176. }
  177. /**
  178. * 页面: 修改&查看 活动基础设置 doPageShow
  179. * 地址:http://cas.lishuy.com/?c=Admin_Activity_SetBasics&a=PageShow&events_id=nnn
  180. * 参数:events_id 活动ID
  181. */
  182. public function doPageShow() {
  183. $events_id = Request::g('events_id');
  184. $eventData =$this->objLottery->getOneLotteryEvents($events_id);
  185. $typeData = LotteryEvents::getType ();
  186. $bigTypeConfig = LotteryEvents::getBigTypeConfig ();
  187. $platformList = array ();
  188. $objDPlatform = new Platform ();
  189. $platformList = $objDPlatform->getsAll ();
  190. if (!self::isSuperadmin()) {
  191. $objDBackUser = new DBackUser();
  192. $user = $objDBackUser->get(self::getUid());
  193. $platfrom = $user['platfrom'];
  194. $tmpPlatformList = array();
  195. foreach ($platfrom as $tmpPlatformId) {
  196. $tmpPlatformList[] = $platformList[$tmpPlatformId];
  197. }
  198. $platformList = $tmpPlatformList;
  199. }
  200. $authorize = LotteryEvents::getAuthorize ();
  201. // 获取是是否在列表中显示配置
  202. $listDisplay = LotteryEvents::getListDisplay ();
  203. // 是否推荐到焦点图
  204. $focusDisplay = LotteryEvents::getFocusDisplay ();
  205. // 是否置顶
  206. $topDisplay = LotteryEvents::getTopDisplay ();
  207. $objDCategory = new Category ();
  208. $categoryList = $objDCategory->getsAll ();
  209. $getMusicPosition = LotteryEvents::getMusicPosition ();
  210. $navConfig = $this->getUpPageNav($events_id, 'Basics');
  211. // 微信相关模块 不包含类型
  212. $weixinFormNotType = array(
  213. LotteryEvents::TYPE_EVENTS_OTHER,
  214. );
  215. $this->setOutput ( 'weixinFormNotType', $weixinFormNotType );
  216. // 音乐相关 不包含类型
  217. $musicFormNotType = array(
  218. LotteryEvents::TYPE_EVENTS_CUT_SCREEN,
  219. );
  220. $this->setOutput ( 'musicFormNotType', $musicFormNotType );
  221. // 切屏方向 不包含的类型
  222. $cutDirectionFormNotType = array(
  223. LotteryEvents::TYPE_EVENTS_CUT_SCREEN,
  224. );
  225. $this->setOutput ( 'cutDirectionFormNotType', $cutDirectionFormNotType );
  226. // 发货状态 不包含的类型
  227. $deliveryFormNotType = array(
  228. LotteryEvents::TYPE_EVENTS_CUT_SCREEN,
  229. LotteryEvents::TYPE_EVENTS_ARTICLE,
  230. LotteryEvents::TYPE_EVENTS_OTHER,
  231. LotteryEvents::TYPE_EVENTS_CODE,
  232. LotteryEvents::TYPE_EVENTS_INVITATION,
  233. LotteryEvents::TYPE_EVENTS_VOTE,
  234. );
  235. $this->setOutput ( 'deliveryFormNotType', $deliveryFormNotType );
  236. $deliveryDesc = LotteryEvents::getDeliveryType (); // 发货状态
  237. if(!$eventData['delivery']){
  238. $eventData['delivery'] = LotteryEvents::DELIVERY_FALSE;
  239. }
  240. $this->setOutput ( 'deliveryDesc', $deliveryDesc );
  241. $this->tpl = 'admin/activity/page_show';
  242. $title = '基础设置';
  243. $this->setOutput('title', $title);
  244. $this->setOutput('menu_active', array('name' => 'mypublish', 'item' => '')); //激活菜单
  245. $this->addNavMenu('活动列表');
  246. $this->addNavMenu($title);
  247. $objDPlatform = new Platform();
  248. $platFormData = $objDPlatform -> getsAll();
  249. # 获取当前活动都添加哪些 列表
  250. # 返回列表IDs
  251. $platFormIds = array();
  252. foreach($platFormData as $key => $data){
  253. if($eventData['platform_ids']&$key){
  254. $platFormIds[$key] = $key;
  255. }
  256. }
  257. $this->setOutput('displayDesc', LotteryEvents::getDisplay());
  258. $this->setOutput ( 'navConfig', $navConfig ); // 导航显示 URL 配置
  259. $this->setOutput ( 'getMusicPosition', $getMusicPosition );// 音乐图标位置
  260. $this->setOutput ( 'platformList', $platformList );// 获取所有发布列表(平台列表)
  261. $this->setOutput ( 'categoryList', $categoryList );// 获取所有发布列表(平台列表)
  262. $this->setOutput ( 'listDisplay', $listDisplay ); // 获取是否在列表中显示
  263. $this->setOutput ( 'focusDisplay', $focusDisplay );
  264. $this->setOutput ( 'topDisplay', $topDisplay );
  265. $this->setOutput ( 'authorize', $authorize );// 获取是否登录配置参数配置
  266. $this->setOutput ( 'typeData', $typeData ); // 所有活动类型
  267. $this->setOutput ( 'thisType', $eventData['type'] ); // 所有活动类型
  268. $this->setOutput ( 'bigTypeConfig', $bigTypeConfig ); // 所有活动类型
  269. $this->setOutput ( 'eventData', $eventData ); // 当前活动详情
  270. $this->setOutput('platFormIds', $platFormIds);
  271. $this->setOutput('pagePublicData', $this->getPagePublicData($events_id)); // 后台管理相关数据
  272. }
  273. /**
  274. * 事件: 修改活动基础设置 doReqUp
  275. * 地址:http://cas.lishuy.com/?c=Admin_Activity_SetBasics&a=PageShow&events_id=nnn
  276. * 参数:见方法内
  277. */
  278. public function doReqUp() {
  279. $events_id = Request::p('events_id');
  280. $info ['img_url'] = Request::p('img_url'); // 活动头图 img_url 表单名称 img_url
  281. // $tmpImgData = $this->loadImg ( 'img_url' );
  282. // if (is_array ( $tmpImgData )) {
  283. // $info ['img_url'] = $tmpImgData ['url'] [0];
  284. // }
  285. $info ['events_name'] = Request::p ( 'events_name' ); // 活动标题 events_name 时间戳
  286. $info ['begin_time'] = Request::p ( 'begin_time' ) ? Request::p ( 'begin_time' ) : '2025-01.01 00:00:00'; // 活动开始时间 begin_time 时间戳
  287. $info ['begin_time'] = strtotime($info ['begin_time']);
  288. $info ['end_time'] = Request::p ( 'end_time' ) ? Request::p ( 'end_time' ) : '2025-01.01 00:00:00'; // 活动结束时间 end_time
  289. $info ['end_time'] = strtotime($info ['end_time']);
  290. /*
  291. $tmp_platform_ids = $_POST['platform_ids'];
  292. foreach ( $tmp_platform_ids as $platform_id ) {
  293. $info ['platform_ids'] += $platform_id;
  294. }
  295. */
  296. $info ['weixinShare'] ['imgUrl'] = Request::p('wx_img_url'); // 分享图片 weixinShare[imgUrl] 表单名称 wx_img_url
  297. // $tmpImgData = $this->loadImg ( 'wx_img_url' );
  298. // if (is_array ( $tmpImgData )) {
  299. // $info ['weixinShare'] ['imgUrl'] = $tmpImgData ['url'] [0];
  300. // }
  301. $info ['weixinShare'] ['title'] = Request::p ( 'weixinShare_title' ); // 分享标题 weixinShare[title]
  302. $info ['weixinShare'] ['content'] = Request::p ( 'weixinShare_content' ); // 分享内容 weixinShare[content]
  303. $info ['weixinShare'] ['jump_url'] = Request::p ( 'weixinShare_jump_url' ); // 分享后跳转地址 weixinShare[jump_url]
  304. $info ['authorize'] = Request::p ( 'authorize' ); // 是否需要登录 authorize
  305. $info ['list_display'] = Request::p ( 'list_display' ); // 是否在列表中出现
  306. $info ['top_display'] = Request::p ( 'top_display' );
  307. $info ['focus_display'] = Request::p ( 'focus_display' );
  308. $info ['category_id'] = Request::p ( 'category_id' );
  309. $info ['delivery'] = Request::p ( 'delivery' ); // 是否发货
  310. $info ['participation_user_num'] = Request::p ( 'participation_user_num' ); // 每天用户参与次数 participation_user_num
  311. $info ['participation_ip_num'] = Request::p ( 'participation_ip_num' ); // 允许每天IP参与次数 participation_ip_num
  312. $info ['participation_user_all_num'] = Request::p ( 'participation_user_all_num' ); // 允许用户参与次数 participation_user_all_num
  313. $info ['winning_user_num'] = Request::p ( 'winning_user_num' ); // 允许用户获奖次数 winning_user_num
  314. $info['cut_fx'] = Request::p('cut_fx'); // 切屏方向
  315. $info ['music_url'] = Request::p('music_url'); // 获取背景音乐 music_url 表单名称:music_url
  316. $info['loading_bg_color'] = Request::p('loading_bg_color');
  317. $info['loading_text_color'] = Request::p('loading_text_color');
  318. // $tmpMusicData = $this->loadMusic ( 'music' );
  319. // if (is_array ( $tmpMusicData )) {
  320. // $info ['music_url'] = $tmpMusicData ['url'] [0];
  321. // }
  322. $info ['music_position'] = Request::p ( 'music_position' ); // 音乐图标位置 music_position
  323. $display = Request::p ( 'display' );
  324. if($display!=null){
  325. $info ['display'] = $display;
  326. }
  327. # 修改操作
  328. $tmpData = $this->objLottery->upLotteryEvents($info, array('id' => $events_id));
  329. if(!$tmpData){
  330. $this->ajax_fail_exit('修改失败');
  331. }
  332. $this->setNowUpPage($events_id, 'Basics');
  333. $this->ajax_success_exit(Request::schemeDomain () . "/?c=Admin_Activity_SetBasics&a=PageShow&events_id={$events_id}");
  334. }
  335. /**
  336. * 上传音乐
  337. *
  338. * @param string $files_name
  339. */
  340. public function loadMusic($files_name = 'music') {
  341. $uploadConfig = Config::getInstance ()->get ( 'upload' );
  342. // 定义允许上传的文件扩展名
  343. $ext_arr = array (
  344. 'music' => array (
  345. 'mp3'
  346. )
  347. );
  348. if (! is_array ( $_FILES [$image_name] ['name'] )) {
  349. foreach ( $_FILES [$image_name] as $key => $data ) {
  350. unset ( $_FILES [$image_name] [$key] );
  351. $_FILES [$image_name] [$key] [] = $data;
  352. }
  353. }
  354. $file_url = array ();
  355. foreach ( $_FILES [$image_name] ['name'] as $key => $oneData ) {
  356. // 文件保存目录路径
  357. $save_path = $uploadConfig ['path'] . '/';
  358. $save_path = realpath ( $save_path ) . '/';
  359. // PHP上传失败
  360. if ($_FILES [$image_name] ['error'] [$key] != 0) {
  361. switch ($_FILES [$image_name] ['error'] [$key]) {
  362. case '1' :
  363. $error = '超过php.ini允许的大小。';
  364. break;
  365. case '2' :
  366. $error = '超过表单允许的大小。';
  367. break;
  368. case '3' :
  369. $error = '音乐只有部分被上传。';
  370. break;
  371. case '4' :
  372. $error = '请选择音乐文件。';
  373. break;
  374. case '6' :
  375. $error = '找不到临时目录。';
  376. break;
  377. case '7' :
  378. $error = '写文件到硬盘出错。';
  379. break;
  380. case '8' :
  381. $error = 'File upload stopped by extension。';
  382. break;
  383. case '999' :
  384. default :
  385. $error = '未知错误。';
  386. }
  387. return $error;
  388. }
  389. // 有上传文件时
  390. if (empty ( $_FILES ) === false) {
  391. // 原文件名
  392. $file_name = $_FILES [$image_name] ['name'] [$key];
  393. // 服务器上临时文件名
  394. $tmp_name = $_FILES [$image_name] ['tmp_name'] [$key];
  395. // 文件大小
  396. $file_size = $_FILES [$image_name] ['size'] [$key];
  397. // 检查文件名
  398. if (! $file_name) {
  399. return "请选择文件。";
  400. }
  401. // 检查目录
  402. if (@is_dir ( $save_path ) === false) {
  403. return "上传目录不存在。";
  404. }
  405. // 检查目录写权限
  406. if (@is_writable ( $save_path ) === false) {
  407. return "上传目录没有写权限。";
  408. }
  409. // 检查是否已上传
  410. if (@is_uploaded_file ( $tmp_name ) === false) {
  411. return "上传失败。";
  412. }
  413. // 检查目录名
  414. $dir_name = 'music';
  415. if (empty ( $ext_arr [$dir_name] )) {
  416. return "目录名不正确。";
  417. }
  418. // 获得文件扩展名
  419. $temp_arr = explode ( ".", $file_name );
  420. $file_ext = array_pop ( $temp_arr );
  421. $file_ext = trim ( $file_ext );
  422. $file_ext = strtolower ( $file_ext );
  423. // 检查扩展名
  424. if (in_array ( $file_ext, $ext_arr [$dir_name] ) === false) {
  425. return "上传文件扩展名是不允许的扩展名。\n只允许" . implode ( ",", $ext_arr [$dir_name] ) . "格式。";
  426. }
  427. // 创建文件夹
  428. $ymd = date ( "Ymd" );
  429. $ymd = date ( "Y" ) . '/' . date ( 'md' );
  430. // 文件保存目录URL
  431. $save_url = $uploadConfig ['url'] . '/';
  432. $new_file_name = '';
  433. $save_path .= $dir_name . "/" . $ymd . "/";
  434. $save_url .= $dir_name . "/" . $ymd . "/";
  435. if (! file_exists ( $save_path )) {
  436. if (! mkdir ( $save_path, 0775, true )) {
  437. return "创建目录失败:{$save_path}";
  438. }
  439. }
  440. // 新文件名
  441. $new_file_name = date ( "His" ) . '_' . rand ( 10000, 99999 ) . '.' . $file_ext;
  442. // 移动文件
  443. $file_path = $save_path . $new_file_name;
  444. // 原图上传
  445. if (move_uploaded_file ( $tmp_name, $file_path ) === false) {
  446. return "上传文件失败。";
  447. }
  448. @chmod ( $file_path, 0777 );
  449. $file_url [$key] = $file_path;
  450. }
  451. }
  452. return array (
  453. 'error' => 0,
  454. 'url' => $file_url
  455. );
  456. }
  457. /**
  458. * 方法:图片上传
  459. */
  460. public function loadImg($image_name = 'image') {
  461. $uploadConfig = Config::getInstance ()->get ( 'upload' );
  462. // 定义允许上传的文件扩展名
  463. $ext_arr = array (
  464. 'image' => array (
  465. 'gif',
  466. 'jpg',
  467. 'jpeg',
  468. 'png',
  469. 'bmp'
  470. )
  471. );
  472. if (! is_array ( $_FILES [$image_name] ['name'] )) {
  473. foreach ( $_FILES [$image_name] as $key => $data ) {
  474. unset ( $_FILES [$image_name] [$key] );
  475. $_FILES [$image_name] [$key] [] = $data;
  476. }
  477. }
  478. $file_url = array ();
  479. foreach ( $_FILES [$image_name] ['name'] as $key => $oneData ) {
  480. // 文件保存目录路径
  481. $save_path = $uploadConfig ['path'] . '/';
  482. $save_path = realpath ( $save_path ) . '/';
  483. // PHP上传失败
  484. if ($_FILES [$image_name] ['error'] [$key] != 0) {
  485. switch ($_FILES [$image_name] ['error'] [$key]) {
  486. case '1' :
  487. $error = '超过php.ini允许的大小。';
  488. break;
  489. case '2' :
  490. $error = '超过表单允许的大小。';
  491. break;
  492. case '3' :
  493. $error = '图片只有部分被上传。';
  494. break;
  495. case '4' :
  496. $error = '请选择图片。';
  497. break;
  498. case '6' :
  499. $error = '找不到临时目录。';
  500. break;
  501. case '7' :
  502. $error = '写文件到硬盘出错。';
  503. break;
  504. case '8' :
  505. $error = 'File upload stopped by extension。';
  506. break;
  507. case '999' :
  508. default :
  509. $error = '未知错误。';
  510. }
  511. return $error;
  512. }
  513. // 有上传文件时
  514. if (empty ( $_FILES ) === false) {
  515. // 原文件名
  516. $file_name = $_FILES [$image_name] ['name'] [$key];
  517. // 服务器上临时文件名
  518. $tmp_name = $_FILES [$image_name] ['tmp_name'] [$key];
  519. // 文件大小
  520. $file_size = $_FILES [$image_name] ['size'] [$key];
  521. // 检查文件名
  522. if (! $file_name) {
  523. return "请选择文件。";
  524. }
  525. // 检查目录
  526. if (@is_dir ( $save_path ) === false) {
  527. return "上传目录不存在。";
  528. }
  529. // 检查目录写权限
  530. if (@is_writable ( $save_path ) === false) {
  531. return "上传目录没有写权限。";
  532. }
  533. // 检查是否已上传
  534. if (@is_uploaded_file ( $tmp_name ) === false) {
  535. return "上传失败。";
  536. }
  537. // 检查目录名
  538. $dir_name = 'image';
  539. if (empty ( $ext_arr [$dir_name] )) {
  540. return "目录名不正确。";
  541. }
  542. // 获得文件扩展名
  543. $temp_arr = explode ( ".", $file_name );
  544. $file_ext = array_pop ( $temp_arr );
  545. $file_ext = trim ( $file_ext );
  546. $file_ext = strtolower ( $file_ext );
  547. // 检查扩展名
  548. if (in_array ( $file_ext, $ext_arr [$dir_name] ) === false) {
  549. return "上传文件扩展名是不允许的扩展名。\n只允许" . implode ( ",", $ext_arr [$dir_name] ) . "格式。";
  550. }
  551. // 创建文件夹
  552. $ymd = date ( "Ymd" );
  553. $ymd = date ( "Y" ) . '/' . date ( 'md' );
  554. // 文件保存目录URL
  555. $save_url = $uploadConfig ['url'] . '/';
  556. $new_file_name = '';
  557. $save_path .= $dir_name . "/" . $ymd . "/";
  558. $save_url .= $dir_name . "/" . $ymd . "/";
  559. if (! file_exists ( $save_path )) {
  560. if (! mkdir ( $save_path, 0775, true )) {
  561. return "创建目录失败:{$save_path}";
  562. }
  563. }
  564. // 新文件名
  565. $new_file_name = date ( "His" ) . '_' . rand ( 10000, 99999 ) . '.' . $file_ext;
  566. // 移动文件
  567. $file_path = $save_path . $new_file_name;
  568. // 原图上传
  569. if (move_uploaded_file ( $tmp_name, $file_path ) === false) {
  570. return "上传文件失败。";
  571. }
  572. error_reporting ( E_ALL );
  573. // 压缩65质量原图
  574. $this_basename = basename ( $file_path );
  575. $this_dirname = dirname ( $file_path );
  576. $z_file_path = $this_dirname . '/' . 'z.' . $this_basename;
  577. Image::transformFormat ( $file_path, $z_file_path, 'JPG' );
  578. @chmod ( $file_path, 0777 );
  579. $file_url [$key] = $save_url . 'z.' . $new_file_name;
  580. }
  581. }
  582. return array (
  583. 'error' => 0,
  584. 'url' => $file_url
  585. );
  586. }
  587. public function display() {
  588. return $this->render ();
  589. }
  590. }