FileService.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace service;
  12. class FileService
  13. {
  14. /*
  15. @function 创建目录
  16. @var:$filename 目录名
  17. @return: true
  18. */
  19. static public function mk_dir($dir)
  20. {
  21. $dir = rtrim($dir, '/') . '/';
  22. if (!is_dir($dir)) {
  23. if (mkdir($dir, 0700) == false) {
  24. return false;
  25. }
  26. return true;
  27. }
  28. return true;
  29. }
  30. /*
  31. @function 写文件
  32. @var:$filename 文件名
  33. @var:$writetext 文件内容
  34. @var:$openmod 打开方式
  35. @return: 成功=true
  36. */
  37. static function write_file($filename, $writetext, $openmod = 'w')
  38. {
  39. if (@$fp = fopen($filename, $openmod)) {
  40. flock($fp, 2);
  41. fwrite($fp, $writetext);
  42. fclose($fp);
  43. return true;
  44. } else {
  45. return false;
  46. }
  47. }
  48. /*
  49. @function 删除目录
  50. @var:$dirName 原目录
  51. @return: 成功=true
  52. */
  53. static function del_dir($dirName)
  54. {
  55. if (!file_exists($dirName)) {
  56. return false;
  57. }
  58. $dir = opendir($dirName);
  59. while ($fileName = readdir($dir)) {
  60. $file = $dirName . '/' . $fileName;
  61. if ($fileName != '.' && $fileName != '..') {
  62. if (is_dir($file)) {
  63. self::del_dir($file);
  64. } else {
  65. unlink($file);
  66. }
  67. }
  68. }
  69. closedir($dir);
  70. return rmdir($dirName);
  71. }
  72. /*
  73. @function 复制目录
  74. @var:$surDir 原目录
  75. @var:$toDir 目标目录
  76. @return: true
  77. */
  78. public function copy_dir($surDir, $toDir)
  79. {
  80. $surDir = rtrim($surDir, '/') . '/';
  81. $toDir = rtrim($toDir, '/') . '/';
  82. if (!file_exists($surDir)) {
  83. return false;
  84. }
  85. if (!file_exists($toDir)) {
  86. $this->create_dir($toDir);
  87. }
  88. $file = opendir($surDir);
  89. while ($fileName = readdir($file)) {
  90. $file1 = $surDir . '/' . $fileName;
  91. $file2 = $toDir . '/' . $fileName;
  92. if ($fileName != '.' && $fileName != '..') {
  93. if (is_dir($file1)) {
  94. self::copy_dir($file1, $file2);
  95. } else {
  96. copy($file1, $file2);
  97. }
  98. }
  99. }
  100. closedir($file);
  101. return true;
  102. }
  103. /*
  104. @function 列出目录
  105. @var:$dir 目录名
  106. @return: 目录数组
  107. 列出文件夹下内容,返回数组 $dirArray['dir']:存文件夹;$dirArray['file']:存文件
  108. */
  109. static function get_dirs($dir)
  110. {
  111. $dir = rtrim($dir, '/') . '/';
  112. $dirArray [][] = NULL;
  113. if (false != ($handle = opendir($dir))) {
  114. $i = 0;
  115. $j = 0;
  116. while (false !== ($file = readdir($handle))) {
  117. if (is_dir($dir . $file)) { //判断是否文件夹
  118. $dirArray ['dir'] [$i] = $file;
  119. $i++;
  120. } else {
  121. $dirArray ['file'] [$j] = $file;
  122. $j++;
  123. }
  124. }
  125. closedir($handle);
  126. }
  127. return $dirArray;
  128. }
  129. /*
  130. @function 统计文件夹大小
  131. @var:$dir 目录名
  132. @return: 文件夹大小(单位 B)
  133. */
  134. static function get_size($dir)
  135. {
  136. $dirlist = opendir($dir);
  137. $dirsize = 0;
  138. while (false !== ($folderorfile = readdir($dirlist))) {
  139. if ($folderorfile != "." && $folderorfile != "..") {
  140. if (is_dir("$dir/$folderorfile")) {
  141. $dirsize += self::get_size("$dir/$folderorfile");
  142. } else {
  143. $dirsize += filesize("$dir/$folderorfile");
  144. }
  145. }
  146. }
  147. closedir($dirlist);
  148. return $dirsize;
  149. }
  150. /*
  151. @function 检测是否为空文件夹
  152. @var:$dir 目录名
  153. @return: 存在则返回true
  154. */
  155. static function empty_dir($dir)
  156. {
  157. return (($files = @scandir($dir)) && count($files) <= 2);
  158. }
  159. /**
  160. * 创建多级目录
  161. * @param string $dir
  162. * @param int $mode
  163. * @return boolean
  164. */
  165. public function create_dir($dir, $mode = 0777)
  166. {
  167. return is_dir($dir) or ($this->create_dir(dirname($dir)) and mkdir($dir, $mode));
  168. }
  169. /**
  170. * 创建指定路径下的指定文件
  171. * @param string $path (需要包含文件名和后缀)
  172. * @param boolean $over_write 是否覆盖文件
  173. * @param int $time 设置时间。默认是当前系统时间
  174. * @param int $atime 设置访问时间。默认是当前系统时间
  175. * @return boolean
  176. */
  177. public function create_file($path, $over_write = FALSE, $time = NULL, $atime = NULL)
  178. {
  179. $path = $this->dir_replace($path);
  180. $time = empty($time) ? time() : $time;
  181. $atime = empty($atime) ? time() : $atime;
  182. if (file_exists($path) && $over_write) {
  183. $this->unlink_file($path);
  184. }
  185. $aimDir = dirname($path);
  186. $this->create_dir($aimDir);
  187. return touch($path, $time, $atime);
  188. }
  189. /**
  190. * 关闭文件操作
  191. * @param string $path
  192. * @return boolean
  193. */
  194. public function close($path)
  195. {
  196. return fclose($path);
  197. }
  198. /**
  199. * 读取文件操作
  200. * @param string $file
  201. * @return boolean
  202. */
  203. public function read_file($file)
  204. {
  205. return @file_get_contents($file);
  206. }
  207. /**
  208. * 确定服务器的最大上传限制(字节数)
  209. * @return int 服务器允许的最大上传字节数
  210. */
  211. public function allow_upload_size()
  212. {
  213. $val = trim(ini_get('upload_max_filesize'));
  214. return $val;
  215. }
  216. /**
  217. * 字节格式化 把字节数格式为 B K M G T P E Z Y 描述的大小
  218. * @param int $size 大小
  219. * @param int $dec 显示类型
  220. * @return int
  221. */
  222. public function byte_format($size, $dec = 2)
  223. {
  224. $a = array("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB");
  225. $pos = 0;
  226. while ($size >= 1024) {
  227. $size /= 1024;
  228. $pos++;
  229. }
  230. return round($size, $dec) . " " . $a[$pos];
  231. }
  232. /**
  233. * 删除非空目录
  234. * 说明:只能删除非系统和特定权限的文件,否则会出现错误
  235. * @param string $dirName 目录路径
  236. * @param boolean $is_all 是否删除所有
  237. * @param boolean $del_dir 是否删除目录
  238. * @return boolean
  239. */
  240. public function remove_dir($dir_path, $is_all = FALSE)
  241. {
  242. $dirName = $this->dir_replace($dir_path);
  243. $handle = @opendir($dirName);
  244. while (($file = @readdir($handle)) !== FALSE) {
  245. if ($file != '.' && $file != '..') {
  246. $dir = $dirName . '/' . $file;
  247. if ($is_all) {
  248. is_dir($dir) ? $this->remove_dir($dir) : $this->unlink_file($dir);
  249. } else {
  250. if (is_file($dir)) {
  251. $this->unlink_file($dir);
  252. }
  253. }
  254. }
  255. }
  256. closedir($handle);
  257. return @rmdir($dirName);
  258. }
  259. /**
  260. * 获取完整文件名
  261. * @param string $fn 路径
  262. * @return string
  263. */
  264. public function get_basename($file_path)
  265. {
  266. $file_path = $this->dir_replace($file_path);
  267. return basename(str_replace('\\', '/', $file_path));
  268. //return pathinfo($file_path,PATHINFO_BASENAME);
  269. }
  270. /**
  271. * 获取文件后缀名
  272. * @param string $file_name 文件路径
  273. * @return string
  274. */
  275. public function get_ext($file)
  276. {
  277. $file = $this->dir_replace($file);
  278. //return strtolower(substr(strrchr(basename($file), '.'),1));
  279. //return end(explode(".",$filename ));
  280. //return strtolower(trim(array_pop(explode('.', $file))));//取得后缀
  281. //return preg_replace('/.*\.(.*[^\.].*)*/iU','\\1',$file);
  282. return pathinfo($file, PATHINFO_EXTENSION);
  283. }
  284. /**
  285. * 取得指定目录名称
  286. * @param string $path 文件路径
  287. * @param int $num 需要返回以上级目录的数
  288. * @return string
  289. */
  290. public function father_dir($path, $num = 1)
  291. {
  292. $path = $this->dir_replace($path);
  293. $arr = explode('/', $path);
  294. if ($num == 0 || count($arr) < $num) return pathinfo($path, PATHINFO_BASENAME);
  295. return substr(strrev($path), 0, 1) == '/' ? $arr[(count($arr) - (1 + $num))] : $arr[(count($arr) - $num)];
  296. }
  297. /**
  298. * 删除文件
  299. * @param string $path
  300. * @return boolean
  301. */
  302. public function unlink_file($path)
  303. {
  304. $path = $this->dir_replace($path);
  305. if (file_exists($path)) {
  306. return unlink($path);
  307. }
  308. }
  309. /**
  310. * 文件操作(复制/移动)
  311. * @param string $old_path 指定要操作文件路径(需要含有文件名和后缀名)
  312. * @param string $new_path 指定新文件路径(需要新的文件名和后缀名)
  313. * @param string $type 文件操作类型
  314. * @param boolean $overWrite 是否覆盖已存在文件
  315. * @return boolean
  316. */
  317. public function handle_file($old_path, $new_path, $type = 'copy', $overWrite = FALSE)
  318. {
  319. $old_path = $this->dir_replace($old_path);
  320. $new_path = $this->dir_replace($new_path);
  321. if (file_exists($new_path) && $overWrite = FALSE) {
  322. return FALSE;
  323. } else if (file_exists($new_path) && $overWrite = TRUE) {
  324. $this->unlink_file($new_path);
  325. }
  326. $aimDir = dirname($new_path);
  327. $this->create_dir($aimDir);
  328. switch ($type) {
  329. case 'copy':
  330. return copy($old_path, $new_path);
  331. break;
  332. case 'move':
  333. return @rename($old_path, $new_path);
  334. break;
  335. }
  336. }
  337. /**
  338. * 文件夹操作(复制/移动)
  339. * @param string $old_path 指定要操作文件夹路径
  340. * @param string $aimDir 指定新文件夹路径
  341. * @param string $type 操作类型
  342. * @param boolean $overWrite 是否覆盖文件和文件夹
  343. * @return boolean
  344. */
  345. public function handle_dir($old_path, $new_path, $type = 'copy', $overWrite = FALSE)
  346. {
  347. $new_path = $this->check_path($new_path);
  348. $old_path = $this->check_path($old_path);
  349. if (!is_dir($old_path)) return FALSE;
  350. if (!file_exists($new_path)) $this->create_dir($new_path);
  351. $dirHandle = opendir($old_path);
  352. if (!$dirHandle) return FALSE;
  353. $boolean = TRUE;
  354. while (FALSE !== ($file = readdir($dirHandle))) {
  355. if ($file == '.' || $file == '..') continue;
  356. if (!is_dir($old_path . $file)) {
  357. $boolean = $this->handle_file($old_path . $file, $new_path . $file, $type, $overWrite);
  358. } else {
  359. $this->handle_dir($old_path . $file, $new_path . $file, $type, $overWrite);
  360. }
  361. }
  362. switch ($type) {
  363. case 'copy':
  364. closedir($dirHandle);
  365. return $boolean;
  366. break;
  367. case 'move':
  368. closedir($dirHandle);
  369. return @rmdir($old_path);
  370. break;
  371. }
  372. }
  373. /**
  374. * 替换相应的字符
  375. * @param string $path 路径
  376. * @return string
  377. */
  378. public function dir_replace($path)
  379. {
  380. return str_replace('//', '/', str_replace('\\', '/', $path));
  381. }
  382. /**
  383. * 读取指定路径下模板文件
  384. * @param string $path 指定路径下的文件
  385. * @return string $rstr
  386. */
  387. public function get_templtes($path)
  388. {
  389. $path = $this->dir_replace($path);
  390. if (file_exists($path)) {
  391. $fp = fopen($path, 'r');
  392. $rstr = fread($fp, filesize($path));
  393. fclose($fp);
  394. return $rstr;
  395. } else {
  396. return '';
  397. }
  398. }
  399. /**
  400. * 文件重命名
  401. * @param string $oldname
  402. * @param string $newname
  403. */
  404. public function rename($oldname, $newname)
  405. {
  406. if (($newname != $oldname) && is_writable($oldname)) {
  407. return rename($oldname, $newname);
  408. }
  409. }
  410. /**
  411. * 获取指定路径下的信息
  412. * @param string $dir 路径
  413. * @return ArrayObject
  414. */
  415. public function get_dir_info($dir)
  416. {
  417. $handle = @opendir($dir);//打开指定目录
  418. $directory_count = 0;
  419. while (FALSE !== ($file_path = readdir($handle))) {
  420. if ($file_path != "." && $file_path != "..") {
  421. //is_dir("$dir/$file_path") ? $sizeResult += $this->get_dir_size("$dir/$file_path") : $sizeResult += filesize("$dir/$file_path");
  422. $next_path = $dir . '/' . $file_path;
  423. if (is_dir($next_path)) {
  424. $directory_count++;
  425. $result_value = self::get_dir_info($next_path);
  426. $total_size += $result_value['size'];
  427. $file_cout += $result_value['filecount'];
  428. $directory_count += $result_value['dircount'];
  429. } elseif (is_file($next_path)) {
  430. $total_size += filesize($next_path);
  431. $file_cout++;
  432. }
  433. }
  434. }
  435. closedir($handle);//关闭指定目录
  436. $result_value['size'] = $total_size;
  437. $result_value['filecount'] = $file_cout;
  438. $result_value['dircount'] = $directory_count;
  439. return $result_value;
  440. }
  441. /**
  442. * 指定文件编码转换
  443. * @param string $path 文件路径
  444. * @param string $input_code 原始编码
  445. * @param string $out_code 输出编码
  446. * @return boolean
  447. */
  448. public function change_file_code($path, $input_code, $out_code)
  449. {
  450. if (is_file($path))//检查文件是否存在,如果存在就执行转码,返回真
  451. {
  452. $content = file_get_contents($path);
  453. $content = string::chang_code($content, $input_code, $out_code);
  454. $fp = fopen($path, 'w');
  455. return fputs($fp, $content) ? TRUE : FALSE;
  456. fclose($fp);
  457. }
  458. }
  459. /**
  460. * 指定目录下指定条件文件编码转换
  461. * @param string $dirname 目录路径
  462. * @param string $input_code 原始编码
  463. * @param string $out_code 输出编码
  464. * @param boolean $is_all 是否转换所有子目录下文件编码
  465. * @param string $exts 文件类型
  466. * @return boolean
  467. */
  468. public function change_dir_files_code($dirname, $input_code, $out_code, $is_all = TRUE, $exts = '')
  469. {
  470. if (is_dir($dirname)) {
  471. $fh = opendir($dirname);
  472. while (($file = readdir($fh)) !== FALSE) {
  473. if (strcmp($file, '.') == 0 || strcmp($file, '..') == 0) {
  474. continue;
  475. }
  476. $filepath = $dirname . '/' . $file;
  477. if (is_dir($filepath) && $is_all == TRUE) {
  478. $files = $this->change_dir_files_code($filepath, $input_code, $out_code, $is_all, $exts);
  479. } else {
  480. if ($this->get_ext($filepath) == $exts && is_file($filepath)) {
  481. $boole = $this->change_file_code($filepath, $input_code, $out_code, $is_all, $exts);
  482. if (!$boole) continue;
  483. }
  484. }
  485. }
  486. closedir($fh);
  487. return TRUE;
  488. } else {
  489. return FALSE;
  490. }
  491. }
  492. /**
  493. * 列出指定目录下符合条件的文件和文件夹
  494. * @param string $dirname 路径
  495. * @param boolean $is_all 是否列出子目录中的文件
  496. * @param string $exts 需要列出的后缀名文件
  497. * @param string $sort 数组排序
  498. * @return ArrayObject
  499. */
  500. public function list_dir_info($dirname, $is_all = FALSE, $exts = '', $sort = 'ASC')
  501. {
  502. //处理多于的/号
  503. $new = strrev($dirname);
  504. if (strpos($new, '/') == 0) {
  505. $new = substr($new, 1);
  506. }
  507. $dirname = strrev($new);
  508. $sort = strtolower($sort);//将字符转换成小写
  509. $files = array();
  510. $subfiles = array();
  511. if (is_dir($dirname)) {
  512. $fh = opendir($dirname);
  513. while (($file = readdir($fh)) !== FALSE) {
  514. if (strcmp($file, '.') == 0 || strcmp($file, '..') == 0) continue;
  515. $filepath = $dirname . '/' . $file;
  516. switch ($exts) {
  517. case '*':
  518. if (is_dir($filepath) && $is_all == TRUE) {
  519. $files = array_merge($files, self::list_dir_info($filepath, $is_all, $exts, $sort));
  520. }
  521. array_push($files, $filepath);
  522. break;
  523. case 'folder':
  524. if (is_dir($filepath) && $is_all == TRUE) {
  525. $files = array_merge($files, self::list_dir_info($filepath, $is_all, $exts, $sort));
  526. array_push($files, $filepath);
  527. } elseif (is_dir($filepath)) {
  528. array_push($files, $filepath);
  529. }
  530. break;
  531. case 'file':
  532. if (is_dir($filepath) && $is_all == TRUE) {
  533. $files = array_merge($files, self::list_dir_info($filepath, $is_all, $exts, $sort));
  534. } elseif (is_file($filepath)) {
  535. array_push($files, $filepath);
  536. }
  537. break;
  538. default:
  539. if (is_dir($filepath) && $is_all == TRUE) {
  540. $files = array_merge($files, self::list_dir_info($filepath, $is_all, $exts, $sort));
  541. } elseif (preg_match("/\.($exts)/i", $filepath) && is_file($filepath)) {
  542. array_push($files, $filepath);
  543. }
  544. break;
  545. }
  546. switch ($sort) {
  547. case 'asc':
  548. sort($files);
  549. break;
  550. case 'desc':
  551. rsort($files);
  552. break;
  553. case 'nat':
  554. natcasesort($files);
  555. break;
  556. }
  557. }
  558. closedir($fh);
  559. return $files;
  560. } else {
  561. return FALSE;
  562. }
  563. }
  564. /**
  565. * 返回指定路径的文件夹信息,其中包含指定路径中的文件和目录
  566. * @param string $dir
  567. * @return ArrayObject
  568. */
  569. public function dir_info($dir)
  570. {
  571. return scandir($dir);
  572. }
  573. /**
  574. * 判断目录是否为空
  575. * @param string $dir
  576. * @return boolean
  577. */
  578. public function is_empty($dir)
  579. {
  580. $handle = opendir($dir);
  581. while (($file = readdir($handle)) !== false) {
  582. if ($file != '.' && $file != '..') {
  583. closedir($handle);
  584. return true;
  585. }
  586. }
  587. closedir($handle);
  588. return false;
  589. }
  590. /**
  591. * 返回指定文件和目录的信息
  592. * @param string $file
  593. * @return ArrayObject
  594. */
  595. public function list_info($file)
  596. {
  597. $dir = array();
  598. $dir['filename'] = basename($file);//返回路径中的文件名部分。
  599. $dir['pathname'] = realpath($file);//返回绝对路径名。
  600. $dir['owner'] = fileowner($file);//文件的 user ID (所有者)。
  601. $dir['perms'] = fileperms($file);//返回文件的 inode 编号。
  602. $dir['inode'] = fileinode($file);//返回文件的 inode 编号。
  603. $dir['group'] = filegroup($file);//返回文件的组 ID。
  604. $dir['path'] = dirname($file);//返回路径中的目录名称部分。
  605. $dir['atime'] = fileatime($file);//返回文件的上次访问时间。
  606. $dir['ctime'] = filectime($file);//返回文件的上次改变时间。
  607. $dir['perms'] = fileperms($file);//返回文件的权限。
  608. $dir['size'] = filesize($file);//返回文件大小。
  609. $dir['type'] = filetype($file);//返回文件类型。
  610. $dir['ext'] = is_file($file) ? pathinfo($file, PATHINFO_EXTENSION) : '';//返回文件后缀名
  611. $dir['mtime'] = filemtime($file);//返回文件的上次修改时间。
  612. $dir['isDir'] = is_dir($file);//判断指定的文件名是否是一个目录。
  613. $dir['isFile'] = is_file($file);//判断指定文件是否为常规的文件。
  614. $dir['isLink'] = is_link($file);//判断指定的文件是否是连接。
  615. $dir['isReadable'] = is_readable($file);//判断文件是否可读。
  616. $dir['isWritable'] = is_writable($file);//判断文件是否可写。
  617. $dir['isUpload'] = is_uploaded_file($file);//判断文件是否是通过 HTTP POST 上传的。
  618. return $dir;
  619. }
  620. /**
  621. * 返回关于打开文件的信息
  622. * @param $file
  623. * @return ArrayObject
  624. * 数字下标 关联键名(自 PHP 4.0.6) 说明
  625. * 0 dev 设备名
  626. * 1 ino 号码
  627. * 2 mode inode 保护模式
  628. * 3 nlink 被连接数目
  629. * 4 uid 所有者的用户 id
  630. * 5 gid 所有者的组 id
  631. * 6 rdev 设备类型,如果是 inode 设备的话
  632. * 7 size 文件大小的字节数
  633. * 8 atime 上次访问时间(Unix 时间戳)
  634. * 9 mtime 上次修改时间(Unix 时间戳)
  635. * 10 ctime 上次改变时间(Unix 时间戳)
  636. * 11 blksize 文件系统 IO 的块大小
  637. * 12 blocks 所占据块的数目
  638. */
  639. public function open_info($file)
  640. {
  641. $file = fopen($file, "r");
  642. $result = fstat($file);
  643. fclose($file);
  644. return $result;
  645. }
  646. /**
  647. * 改变文件和目录的相关属性
  648. * @param string $file 文件路径
  649. * @param string $type 操作类型
  650. * @param string $ch_info 操作信息
  651. * @return boolean
  652. */
  653. public function change_file($file, $type, $ch_info)
  654. {
  655. switch ($type) {
  656. case 'group' :
  657. $is_ok = chgrp($file, $ch_info);//改变文件组。
  658. break;
  659. case 'mode' :
  660. $is_ok = chmod($file, $ch_info);//改变文件模式。
  661. break;
  662. case 'ower' :
  663. $is_ok = chown($file, $ch_info);//改变文件所有者。
  664. break;
  665. }
  666. }
  667. /**
  668. * 取得文件路径信息
  669. * @param $full_path 完整路径
  670. * @return ArrayObject
  671. */
  672. public function get_file_type($path)
  673. {
  674. //pathinfo() 函数以数组的形式返回文件路径的信息。
  675. //---------$file_info = pathinfo($path); echo file_info['extension'];----------//
  676. //extension取得文件后缀名【pathinfo($path,PATHINFO_EXTENSION)】-----dirname取得文件路径【pathinfo($path,PATHINFO_DIRNAME)】-----basename取得文件完整文件名【pathinfo($path,PATHINFO_BASENAME)】-----filename取得文件名【pathinfo($path,PATHINFO_FILENAME)】
  677. return pathinfo($path);
  678. }
  679. /**
  680. * 取得上传文件信息
  681. * @param $file file属性信息
  682. * @return array
  683. */
  684. public function get_upload_file_info($file)
  685. {
  686. $file_info = $_FILES[$file];//取得上传文件基本信息
  687. $info = array();
  688. $info['type'] = strtolower(trim(stripslashes(preg_replace("/^(.+?);.*$/", "\\1", $file_info['type'])), '"'));//取得文件类型
  689. $info['temp'] = $file_info['tmp_name'];//取得上传文件在服务器中临时保存目录
  690. $info['size'] = $file_info['size'];//取得上传文件大小
  691. $info['error'] = $file_info['error'];//取得文件上传错误
  692. $info['name'] = $file_info['name'];//取得上传文件名
  693. $info['ext'] = $this->get_ext($file_info['name']);//取得上传文件后缀
  694. return $info;
  695. }
  696. /**
  697. * 设置文件命名规则
  698. * @param string $type 命名规则
  699. * @param string $filename 文件名
  700. * @return string
  701. */
  702. public function set_file_name($type)
  703. {
  704. switch ($type) {
  705. case 'hash' :
  706. $new_file = md5(uniqid(mt_rand()));//mt_srand()以随机数md5加密来命名
  707. break;
  708. case 'time' :
  709. $new_file = time();
  710. break;
  711. default :
  712. $new_file = date($type, time());//以时间格式来命名
  713. break;
  714. }
  715. return $new_file;
  716. }
  717. /**
  718. * 文件保存路径处理
  719. * @return string
  720. */
  721. public function check_path($path)
  722. {
  723. return (preg_match('/\/$/', $path)) ? $path : $path . '/';
  724. }
  725. /**
  726. * 文件下载
  727. * $save_dir 保存路径
  728. * $filename 文件名
  729. * @return array
  730. */
  731. public static function down_remote_file($url, $save_dir = '', $filename = '', $type = 0)
  732. {
  733. if (trim($url) == '') {
  734. return array('file_name' => '', 'save_path' => '', 'error' => 1);
  735. }
  736. if (trim($save_dir) == '') {
  737. $save_dir = './';
  738. }
  739. if (trim($filename) == '') {//保存文件名
  740. $ext = strrchr($url, '.');
  741. // if($ext!='.gif'&&$ext!='.jpg'){
  742. // return array('file_name'=>'','save_path'=>'','error'=>3);
  743. // }
  744. $filename = time() . $ext;
  745. }
  746. if (0 !== strrpos($save_dir, '/')) {
  747. $save_dir .= '/';
  748. }
  749. //创建保存目录
  750. if (!file_exists($save_dir) && !mkdir($save_dir, 0777, true)) {
  751. return array('file_name' => '', 'save_path' => '', 'error' => 5);
  752. }
  753. //获取远程文件所采用的方法
  754. if ($type) {
  755. $ch = curl_init();
  756. $timeout = 5;
  757. curl_setopt($ch, CURLOPT_URL, $url);
  758. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  759. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  760. $img = curl_exec($ch);
  761. curl_close($ch);
  762. } else {
  763. ob_start();
  764. readfile($url);
  765. $img = ob_get_contents();
  766. ob_end_clean();
  767. }
  768. //$size=strlen($img);
  769. //文件大小
  770. $fp2 = fopen($save_dir . $filename, 'a');
  771. fwrite($fp2, $img);
  772. fclose($fp2);
  773. unset($img, $url);
  774. return array('file_name' => $filename, 'save_path' => $save_dir . $filename, 'error' => 0);
  775. }
  776. public static function zipopen($filename, $savename)
  777. {
  778. $zip = new \ZipArchive;
  779. $zipfile = $filename;
  780. $res = $zip->open($zipfile);
  781. $toDir = $savename;
  782. if (!file_exists($toDir)) mkdir($toDir, 0777);
  783. $docnum = $zip->numFiles;
  784. for ($i = 0; $i < $docnum; $i++) {
  785. $statInfo = $zip->statIndex($i);
  786. if ($statInfo['crc'] == 0 && $statInfo['comp_size'] != 2) {
  787. //新建目录
  788. mkdir($toDir . '/' . substr($statInfo['name'], 0, -1), 0777);
  789. } else {
  790. //拷贝文件
  791. copy('zip://' . $zipfile . '#' . $statInfo['name'], $toDir . '/' . $statInfo['name']);
  792. }
  793. }
  794. $zip->close();
  795. return true;
  796. }
  797. /**
  798. *设置字体格式
  799. * @param $title string 必选
  800. * return string
  801. */
  802. public static function setUtf8($title)
  803. {
  804. return iconv('utf-8', 'gb2312', $title);
  805. }
  806. /**
  807. *检查指定文件是否能写入
  808. * @param $file string 必选
  809. * return boole
  810. */
  811. public static function isWritable($file)
  812. {
  813. $file = str_replace('\\', '/', $file);
  814. if (!file_exists($file)) return false;
  815. return is_writable($file);
  816. }
  817. }