Uploadify.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. <?php
  2. /**
  3. * 易优CMS
  4. * ============================================================================
  5. * 版权所有 2016-2028 海南赞赞网络科技有限公司,并保留所有权利。
  6. * 网站地址: http://www.eyoucms.com
  7. * ----------------------------------------------------------------------------
  8. * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
  9. * ============================================================================
  10. * Author: 小虎哥 <1105415366@qq.com>
  11. * Date: 2018-4-3
  12. */
  13. namespace app\admin\controller;
  14. use think\Db;
  15. use app\common\logic\ArctypeLogic;
  16. class Uploadify extends Base
  17. {
  18. public $image_type = '';
  19. /**
  20. * 析构函数
  21. */
  22. function __construct()
  23. {
  24. parent::__construct();
  25. $this->image_type = tpCache('basic.image_type');
  26. $this->image_type = !empty($this->image_type) ? str_replace('|', ',', $this->image_type) : 'jpg,gif,png,bmp,jpeg,ico';
  27. }
  28. /**
  29. * 通用的上传图片
  30. */
  31. public function upload()
  32. {
  33. $func = input('func');
  34. $path = input('path','allimg');
  35. $num = input('num/d', '1');
  36. $default_size = intval(tpCache('basic.file_size') * 1024 * 1024); // 单位为b
  37. $size = input('size/d'); // 单位为kb
  38. $size = empty($size) ? $default_size : $size*1024;
  39. $info = array(
  40. 'num' => $num,
  41. 'title' => '',
  42. 'upload' => url('Ueditor/imageUp',array('savepath'=>$path,'pictitle'=>'banner','dir'=>'images')),
  43. 'fileList' => url('Uploadify/fileList',array('path'=>$path)),
  44. 'size' => $size,
  45. 'type' => $this->image_type,
  46. 'input' => input('input'),
  47. 'func' => empty($func) ? 'undefined' : $func,
  48. 'path' => $path,
  49. );
  50. $this->assign('info',$info);
  51. return $this->fetch();
  52. }
  53. /**
  54. * 图库在线管理 - 左侧树形目录结构
  55. */
  56. public function picture_folder()
  57. {
  58. $func = input('func');
  59. $path = input('path','allimg');
  60. $num = input('num/d', '1');
  61. $default_size = intval(tpCache('basic.file_size') * 1024 * 1024); // 单位为b
  62. $size = input('size/d'); // 单位为kb
  63. $size = empty($size) ? $default_size : $size*1024;
  64. $info = array(
  65. 'num' => $num,
  66. 'title' => '',
  67. 'upload' => url('Ueditor/imageUp',array('savepath'=>$path,'pictitle'=>'banner','dir'=>'images')),
  68. 'fileList' => url('Uploadify/fileList',array('path'=>$path)),
  69. 'size' => $size,
  70. 'type' => $this->image_type,
  71. 'input' => input('input'),
  72. 'func' => empty($func) ? 'undefined' : $func,
  73. 'path' => $path,
  74. );
  75. $this->assign('info',$info);
  76. // 侧边栏目录栏目
  77. $dirArr = $this->getDir('uploads');
  78. $dirArr2 = [];
  79. foreach ($dirArr as $key => $val) {
  80. $dirArr2[$val['id']] = $val['dirpath'];
  81. }
  82. foreach ($dirArr as $key => $val) {
  83. $dirfileArr = glob("{$val['dirpath']}/*");
  84. if (empty($dirfileArr)) {
  85. empty($dirfileArr) && @rmdir($val['dirpath']);
  86. $dirArr[$key] = [];
  87. continue;
  88. }
  89. /*图库显示数量*/
  90. $countFile = 0;
  91. $dirfileArr2 = glob("{$val['dirpath']}/*.*"); // 文件数量
  92. $countFile = count($dirfileArr2);
  93. /*end*/
  94. $dirname = preg_replace('/([^\/]+)$/i', '', $val['dirpath']);
  95. $arr_key = array_search(trim($dirname, '/'), $dirArr2);
  96. if (!empty($arr_key)) {
  97. $dirArr[$key]['pId'] = $arr_key;
  98. } else {
  99. $dirArr[$key]['pId'] = 0;
  100. }
  101. $dirArr[$key]['name'] = preg_replace('/^(.*)\/([^\/]+)$/i', '${2}', $val['dirpath']);
  102. !empty($countFile) && $dirArr[$key]['name'] .= "({$countFile})"; // 图库显示数量
  103. }
  104. $zNodes = json_encode($dirArr,true);
  105. $this->assign('zNodes', $zNodes);
  106. return $this->fetch();
  107. }
  108. /**
  109. * 图库在线管理 - 图片列表显示
  110. */
  111. public function get_images_path($images_path = 'uploads')
  112. {
  113. if ('uploads' != $images_path && !preg_match('#^(uploads)/(.*)$#i', $images_path)) {
  114. $this->error('非法访问!');
  115. }
  116. $func = input('func/s');
  117. $num = input('num/d', '1');
  118. $info = array(
  119. 'num' => $num,
  120. 'func' => empty($func) ? 'undefined' : $func,
  121. );
  122. $this->assign('info',$info);
  123. // 常用图片
  124. $common_pic = [];
  125. $arr1 = explode('/', $images_path);
  126. if (1 >= count($arr1)) { // 只有一级目录才显示常用图片
  127. $where = [
  128. 'lang' => $this->admin_lang,
  129. ];
  130. $common_pic = M('common_pic')->where($where)->order('id desc')->limit(6)->field('pic_path')->select();
  131. }
  132. $this->assign('common_pic', $common_pic);
  133. // 图片列表
  134. $images_data = glob($images_path.'/*');
  135. $list = [];
  136. if (!empty($images_data)) {
  137. // 图片类型数组
  138. $image_ext = explode(',', config('global.image_ext'));
  139. // 处理图片
  140. foreach ($images_data as $key => $file) {
  141. $fileArr = explode('.', $file);
  142. $ext = end($fileArr);
  143. $ext = strtolower($ext);
  144. if (in_array($ext, $image_ext)) {
  145. $list[$key]['path'] = ROOT_DIR.'/'.$file;
  146. $list[$key]['time'] = @filemtime($file);
  147. }
  148. }
  149. }
  150. // 图片选择的时间从大到小排序
  151. $list_time = get_arr_column($list,'time');
  152. array_multisort($list_time,SORT_DESC,$list);
  153. // 返回数据
  154. $this->assign('list', $list);
  155. $this->assign('path_directory', $images_path);
  156. return $this->fetch();
  157. }
  158. /**
  159. * 记录常用图片
  160. */
  161. public function update_pic()
  162. {
  163. if(IS_AJAX_POST){
  164. $param = input('param.');
  165. if (!empty($param['images_array'])) {
  166. $commonPic_db = Db::name('common_pic');
  167. $where = '';
  168. $data = [];
  169. foreach ($param['images_array'] as $key => $value) {
  170. // 删除条件
  171. if ($key > '0') { $where .= ','; }
  172. $where .= $value;
  173. // 添加数组
  174. $data[$key] = [
  175. 'pic_path' => $value,
  176. 'lang' => $this->admin_lang,
  177. 'add_time' => getTime(),
  178. 'update_time' => getTime(),
  179. ];
  180. }
  181. // 批量删除选中的图片
  182. $commonPic_db->where('pic_path','IN',$where)->delete();
  183. // 批量添加图片
  184. !empty($data) && $commonPic_db->insertAll($data);
  185. // 查询最后一条数据
  186. $row = $commonPic_db->order('id desc')->limit('20,1')->field('id')->select();
  187. if (!empty($row)) {
  188. $id = $row[0]['id'];
  189. // 删除ID往后的数据
  190. $where_ = array(
  191. 'id' => array('<',$id),
  192. 'lang' => $this->admin_lang,
  193. );
  194. $commonPic_db->where($where_)->delete();
  195. }
  196. }
  197. }
  198. }
  199. /**
  200. * 在弹出窗里的上传图片
  201. */
  202. public function upload_frame()
  203. {
  204. $func = input('func');
  205. $path = input('path','allimg');
  206. $num = input('num/d', '1');
  207. $default_size = intval(tpCache('basic.file_size') * 1024 * 1024); // 单位为b
  208. $size = input('size/d'); // 单位为kb
  209. $size = empty($size) ? $default_size : $size*1024;
  210. $info = array(
  211. 'num'=> $num,
  212. 'title' => '',
  213. 'upload' =>url('Ueditor/imageUp',array('savepath'=>$path,'pictitle'=>'banner','dir'=>'images')),
  214. 'fileList'=>url('Uploadify/fileList',array('path'=>$path)),
  215. 'size' => $size,
  216. 'type' => $this->image_type,
  217. 'input' => input('input'),
  218. 'func' => empty($func) ? 'undefined' : $func,
  219. 'path' => $path,
  220. );
  221. $this->assign('info',$info);
  222. return $this->fetch();
  223. }
  224. /**
  225. * 后台(产品)专用
  226. */
  227. public function upload_product()
  228. {
  229. $aid = input('aid/d');
  230. $func = input('func');
  231. $path = input('path','allimg');
  232. $num = input('num/d', '1');
  233. $default_size = intval(tpCache('basic.file_size') * 1024 * 1024); // 单位为b
  234. $size = input('size/d'); // 单位为kb
  235. $size = empty($size) ? $default_size : $size*1024;
  236. $field = array(
  237. 'aid' => $aid,
  238. 'num' => $num,
  239. 'title' => '',
  240. 'upload' => url('Ueditor/imageUp',array('savepath'=>$path,'pictitle'=>'banner','dir'=>'images')),
  241. 'fileList'=> url('Uploadify/fileList',array('path'=>$path)),
  242. 'size' => $size,
  243. 'type' => $this->image_type,
  244. 'input' => input('input'),
  245. 'func' => empty($func) ? 'undefined' : $func,
  246. 'path' => $path,
  247. );
  248. $this->assign('field',$field);
  249. return $this->fetch();
  250. }
  251. /**
  252. * 完整的上传模板展示
  253. */
  254. public function upload_full()
  255. {
  256. $func = input('func');
  257. $path = input('path','allimg');
  258. $num = input('num/d', '1');
  259. $default_size = intval(tpCache('basic.file_size') * 1024 * 1024); // 单位为b
  260. $size = input('size/d'); // 单位为kb
  261. $size = empty($size) ? $default_size : $size*1024;
  262. $info = array(
  263. 'num'=> $num,
  264. 'title' => '',
  265. 'upload' =>url('Ueditor/imageUp',array('savepath'=>$path,'pictitle'=>'banner','dir'=>'images')),
  266. 'fileList'=>url('Uploadify/fileList',array('path'=>$path)),
  267. 'size' => $size,
  268. 'type' => $this->image_type,
  269. 'input' => input('input'),
  270. 'func' => empty($func) ? 'undefined' : $func,
  271. 'path' => $path,
  272. );
  273. $this->assign('info',$info);
  274. return $this->fetch();
  275. }
  276. /*
  277. * 删除上传的图片
  278. */
  279. public function delupload()
  280. {
  281. if (IS_POST) {
  282. $action = input('action','del');
  283. $filename= input('filename/s');
  284. $filename= empty($filename) ? input('url') : $filename;
  285. $filename= str_replace('../','',$filename);
  286. $filename= trim($filename,'.');
  287. $filename = preg_replace('#^(/[/\w]+)?(/public/upload/|/uploads/|/public/static/admin/logo/)#i', '$2', $filename);
  288. if(eyPreventShell($filename) && $action=='del' && !empty($filename) && file_exists('.'.$filename)){
  289. if (stristr($filename, '/admin/logo/')) {
  290. $filetype = preg_replace('/^(.*)\.(\w+)$/i', '$2', $filename);
  291. $phpfile = strtolower(strstr($filename,'.php')); //排除PHP文件
  292. $size = getimagesize('.'.$filename);
  293. $fileInfo = explode('/',$size['mime']);
  294. if($fileInfo[0] != 'image' || $phpfile || !in_array($filetype, explode(',', config('global.image_ext')))){
  295. exit;
  296. }
  297. if(@unlink('.'.$filename)){
  298. echo 1;
  299. }else{
  300. echo 0;
  301. }
  302. exit;
  303. }
  304. }
  305. echo 1;
  306. exit;
  307. }
  308. }
  309. public function fileList(){
  310. /* 判断类型 */
  311. $type = input('type','Images');
  312. switch ($type){
  313. /* 列出图片 */
  314. case 'Images' : $allowFiles = str_replace(',', '|', $this->image_type);break;
  315. case 'Flash' : $allowFiles = 'flash|swf';break;
  316. /* 列出文件 */
  317. default :
  318. {
  319. $file_type = tpCache('basic.file_type');
  320. $media_type = tpCache('basic.media_type');
  321. $allowFiles = $file_type.'|'.$media_type;
  322. }
  323. }
  324. $listSize = 102400000;
  325. $key = empty($_GET['key']) ? '' : $_GET['key'];
  326. /* 获取参数 */
  327. $size = isset($_GET['size']) ? htmlspecialchars($_GET['size']) : $listSize;
  328. $start = isset($_GET['start']) ? htmlspecialchars($_GET['start']) : 0;
  329. $end = $start + $size;
  330. $path = input('path','allimg');
  331. if (1 == preg_match('#\.#', $path)) {
  332. echo json_encode(array(
  333. "state" => "路径不符合规范",
  334. "list" => array(),
  335. "start" => $start,
  336. "total" => 0
  337. ));
  338. exit;
  339. }
  340. if ('adminlogo' == $path) {
  341. $path = 'public/static/admin/logo';
  342. } else {
  343. $path = UPLOAD_PATH.$path;
  344. }
  345. /* 获取文件列表 */
  346. $files = $this->getfiles($path, $allowFiles, $key);
  347. if (empty($files)) {
  348. echo json_encode(array(
  349. "state" => "没有相关文件",
  350. "list" => array(),
  351. "start" => $start,
  352. "total" => count($files)
  353. ));
  354. exit;
  355. }
  356. /* 获取指定范围的列表 */
  357. $len = count($files);
  358. for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--){
  359. $list[] = $files[$i];
  360. }
  361. /* 返回数据 */
  362. $result = json_encode(array(
  363. "state" => "SUCCESS",
  364. "list" => $list,
  365. "start" => $start,
  366. "total" => count($files)
  367. ));
  368. echo $result;
  369. }
  370. /**
  371. * 遍历获取目录下的指定类型的文件
  372. * @param $path
  373. * @param array $files
  374. * @return array
  375. */
  376. private function getfiles($path, $allowFiles, $key, &$files = array()){
  377. if (!is_dir($path)) return null;
  378. if(substr($path, strlen($path) - 1) != '/') $path .= '/';
  379. $handle = opendir($path);
  380. while (false !== ($file = readdir($handle))) {
  381. if ($file != '.' && $file != '..') {
  382. $path2 = $path . $file;
  383. if (is_dir($path2)) {
  384. $this->getfiles($path2, $allowFiles, $key, $files);
  385. } else {
  386. if (preg_match("/\.(".$allowFiles.")$/i", $file) && preg_match("/.*". $key .".*/i", $file)) {
  387. $files[] = array(
  388. 'url'=> ROOT_DIR.'/'.$path2, // 支持子目录
  389. 'name'=> $file,
  390. 'mtime'=> filemtime($path2)
  391. );
  392. }
  393. }
  394. }
  395. }
  396. return $files;
  397. }
  398. /**
  399. * 提取上传图片目录下的所有图片
  400. *
  401. * @param string $directory 目录路径
  402. * @param string $dir_name 显示的目录前缀路径
  403. * @param array $arr_file 是否删除空目录
  404. * @param num $num 数量
  405. */
  406. private function getDir($directory, &$arr_file = array(), &$num = 0) {
  407. $mydir = glob($directory.'/*', GLOB_ONLYDIR);
  408. $param = input('param.');
  409. if (0 <= $num) {
  410. $dirpathArr = explode('/', $directory);
  411. $level = count($dirpathArr);
  412. $open = (1 >= $level) ? true : false;
  413. $fileList = glob($directory.'/*');
  414. $total = count($fileList); // 目录是否存在任意文件,否则删除该目录
  415. if (!empty($total)) {
  416. $isExistPic = $this->isExistPic($directory);
  417. if (!empty($isExistPic)) {
  418. $arr_file[] = [
  419. 'id' => $num,
  420. 'url' => url('Uploadify/get_images_path',['num'=>$param['num'],'func'=>$param['func'],'lang'=>$param['lang'],'images_path'=>$directory]),
  421. 'target' => 'content_body',
  422. 'isParent' => true,
  423. 'open' => $open,
  424. 'dirpath' => $directory,
  425. 'level' => $level,
  426. 'total' => $total,
  427. ];
  428. }
  429. } else {
  430. @rmdir("$directory");
  431. }
  432. }
  433. if (!empty($mydir)) {
  434. foreach ($mydir as $key => $dir) {
  435. if (stristr("$dir/", 'uploads/soft_tmp/') || stristr("$dir/", 'uploads/tmp/')) {
  436. continue;
  437. }
  438. $num++;
  439. $dirname = str_replace('\\', '/', $dir);
  440. $dirArr = explode('/', $dirname);
  441. $dir = end($dirArr);
  442. $mydir2 = glob("$directory/$dir/*", GLOB_ONLYDIR);
  443. if(!empty($mydir2) AND ($dir != ".") AND ($dir != ".."))
  444. {
  445. $this->getDir("$directory/$dir", $arr_file, $num);
  446. }
  447. else if(($dir != ".") AND ($dir != ".."))
  448. {
  449. $dirpathArr = explode('/', "$directory/$dir");
  450. $level = count($dirpathArr);
  451. $fileList = glob("$directory/$dir/*"); // 目录是否存在任意文件,否则删除该目录
  452. $total = count($fileList);
  453. if (!empty($total)) {
  454. // 目录是否存在图片文件,否则删除该目录
  455. $isExistPic = $this->isExistPic("$directory/$dir");
  456. if (!empty($isExistPic)) {
  457. $arr_file[] = [
  458. 'id' => $num,
  459. 'url' => url('Uploadify/get_images_path',['num'=>$param['num'],'func'=>$param['func'],'lang'=>$param['lang'],'images_path'=>"$directory/$dir"]),
  460. 'target' => 'content_body',
  461. 'isParent' => false,
  462. 'open' => false,
  463. 'dirpath' => "$directory/$dir",
  464. 'level' => $level,
  465. 'icon' => 'public/plugins/ztree/css/zTreeStyle/img/dir_close.png',
  466. 'iconOpen' => 'public/plugins/ztree/css/zTreeStyle/img/dir_open.png',
  467. 'iconClose' => 'public/plugins/ztree/css/zTreeStyle/img/dir_close.png',
  468. 'total' => $total,
  469. ];
  470. }
  471. } else {
  472. @rmdir("$directory/$dir");
  473. }
  474. }
  475. }
  476. }
  477. return $arr_file;
  478. }
  479. /**
  480. * 检测指定目录是否存在图片
  481. *
  482. * @param string $directory 目录路径
  483. * @param string $dir_name 显示的目录前缀路径
  484. * @param array $arr_file 是否删除空目录
  485. * @return boolean
  486. */
  487. private function isExistPic($directory, $dir_name='', &$arr_file = [])
  488. {
  489. if (!file_exists($directory) ) {
  490. return false;
  491. }
  492. if (!empty($arr_file)) {
  493. return true;
  494. }
  495. // 图片类型数组
  496. $image_ext = explode(',', config('global.image_ext'));
  497. $mydir = dir($directory);
  498. while($file = $mydir->read())
  499. {
  500. if((is_dir("$directory/$file")) AND ($file != ".") AND ($file != ".."))
  501. {
  502. if ($dir_name) {
  503. return $this->isExistPic("$directory/$file", "$dir_name/$file", $arr_file);
  504. } else {
  505. return $this->isExistPic("$directory/$file", "$file", $arr_file);
  506. }
  507. }
  508. else if(($file != ".") AND ($file != ".."))
  509. {
  510. $fileArr = explode('.', $file);
  511. $ext = end($fileArr);
  512. $ext = strtolower($ext);
  513. if (in_array($ext, $image_ext)) {
  514. if ($dir_name) {
  515. $arr_file[] = "$dir_name/$file";
  516. } else {
  517. $arr_file[] = "$file";
  518. }
  519. return true;
  520. }
  521. }
  522. }
  523. $mydir->close();
  524. return $arr_file;
  525. }
  526. }