php實現的檔案目錄操作類

來源:互聯網
上載者:User
  1. /**
  2. * 檔案目錄操作類
  3. * 編輯:bbs.it-home.org
  4. * 例子:
  5. * $fileutil = new fileDirUtil();
  6. * $fileutil->createDir('a/1/2/3'); 測試建立檔案夾 建一個a/1/2/3檔案夾
  7. * $fileutil->createFile('b/1/2/3'); 測試建立檔案 在b/1/2/檔案夾下面建一個3檔案
  8. * $fileutil->createFile('b/1/2/3.txt'); 測試建立檔案 在b/1/2/檔案夾下面建一個3.exe檔案
  9. * $fileutil->writeFile('b/1/2/3.txt','this is something i write!'); 在檔案中寫內容
  10. * $arr = $fileutil->readFile2array('example/mysql.txt');
  11. * $arr = $fileutil->readsFile('example/mysql.txt');
  12. * $size=$fileutil->bitSize($fileutil->getDirSize("example")); 得到檔案或目錄的大小
  13. * $fileutil->copyDir('b','d/e'); 測試複製檔案夾 建立一個d/e檔案夾,把b檔案夾下的內容複寫進去
  14. * $fileutil->copyFile('b/1/2/3.exe','b/b/3.exe'); 測試複製檔案 建立一個b/b檔案夾,並把b/1/2檔案夾中的3.exe檔案複製進去
  15. * $fileutil->moveDir('a/','b/c'); 測試移動檔案夾 建立一個b/c檔案夾,並把a檔案夾下的內容移動進去,並刪除a檔案夾
  16. * $fileutil->moveFile('b/1/2/3.exe','b/d/3.exe'); 測試移動檔案 建立一個b/d檔案夾,並把b/1/2中的3.exe移動進去
  17. * $fileutil->unlinkFile('b/d/3.exe'); 測試刪除檔案 刪除b/d/3.exe檔案
  18. * $fileutil->unlinkDir('d'); 測試刪除檔案夾 刪除d檔案夾
  19. * $list = $fileutil->dirList("E:\example"); 測試清單檔案夾 列出目錄下所有檔案
  20. * $list = $fileutil->dirTree("/"); 測試清單資料夾樹狀目錄 列出目錄下所有檔案直接直接的樹關係
  21. */
  22. class fileDirUtil {
  23. /**
  24. * 建立檔案夾
  25. *
  26. * @param string $aimUrl
  27. * @return viod
  28. */
  29. function createDir($aimUrl, $mode = 0777) {
  30. $aimUrl = str_replace ( '', '/', $aimUrl );
  31. $aimDir = '';
  32. $arr = explode ( '/', $aimUrl );
  33. foreach ( $arr as $str ) {
  34. $aimDir .= $str . '/';
  35. if (! file_exists ( $aimDir )) {
  36. mkdir ( $aimDir, $mode );
  37. }
  38. }
  39. }
  40. /**
  41. * 建立檔案
  42. *
  43. * @param string $aimUrl
  44. * @param boolean $overWrite 該參數控制是否覆蓋原檔案
  45. * @return boolean
  46. */
  47. function createFile($aimUrl, $overWrite = false) {
  48. if (file_exists ( $aimUrl ) && $overWrite == false) {
  49. return false;
  50. } elseif (file_exists ( $aimUrl ) && $overWrite == true) {
  51. $this->unlinkFile ( $aimUrl );
  52. }
  53. $aimDir = dirname ( $aimUrl );
  54. $this->createDir ( $aimDir );
  55. touch ( $aimUrl );
  56. return true;
  57. }
  58. /**
  59. * 移動檔案夾
  60. *
  61. * @param string $oldDir
  62. * @param string $aimDir
  63. * @param boolean $overWrite 該參數控制是否覆蓋原檔案
  64. * @return boolean
  65. */
  66. function moveDir($oldDir, $aimDir, $overWrite = false) {
  67. $aimDir = str_replace ( '', '/', $aimDir );
  68. $aimDir = substr ( $aimDir, - 1 ) == '/' ? $aimDir : $aimDir . '/';
  69. $oldDir = str_replace ( '', '/', $oldDir );
  70. $oldDir = substr ( $oldDir, - 1 ) == '/' ? $oldDir : $oldDir . '/';
  71. if (! is_dir ( $oldDir )) {
  72. return false;
  73. }
  74. if (! file_exists ( $aimDir )) {
  75. $this->createDir ( $aimDir );
  76. }
  77. @$dirHandle = opendir ( $oldDir );
  78. if (! $dirHandle) {
  79. return false;
  80. }
  81. while ( false !== ($file = readdir ( $dirHandle )) ) {
  82. if ($file == '.' || $file == '..') {
  83. continue;
  84. }
  85. if (! is_dir ( $oldDir . $file )) {
  86. $this->moveFile ( $oldDir . $file, $aimDir . $file, $overWrite );
  87. } else {
  88. $this->moveDir ( $oldDir . $file, $aimDir . $file, $overWrite );
  89. }
  90. }
  91. closedir ( $dirHandle );
  92. return rmdir ( $oldDir );
  93. }
  94. /**
  95. * 移動檔案
  96. *
  97. * @param string $fileUrl
  98. * @param string $aimUrl
  99. * @param boolean $overWrite 該參數控制是否覆蓋原檔案
  100. * @return boolean
  101. */
  102. function moveFile($fileUrl, $aimUrl, $overWrite = false) {
  103. if (! file_exists ( $fileUrl )) {
  104. return false;
  105. }
  106. if (file_exists ( $aimUrl ) && $overWrite = false) {
  107. return false;
  108. } elseif (file_exists ( $aimUrl ) && $overWrite = true) {
  109. $this->unlinkFile ( $aimUrl );
  110. }
  111. $aimDir = dirname ( $aimUrl );
  112. $this->createDir ( $aimDir );
  113. rename ( $fileUrl, $aimUrl );
  114. return true;
  115. }
  116. /**
  117. * 刪除檔案夾
  118. *
  119. * @param string $aimDir
  120. * @return boolean
  121. */
  122. function unlinkDir($aimDir) {
  123. $aimDir = str_replace ( '', '/', $aimDir );
  124. $aimDir = substr ( $aimDir, - 1 ) == '/' ? $aimDir : $aimDir . '/';
  125. if (! is_dir ( $aimDir )) {
  126. return false;
  127. }
  128. $dirHandle = opendir ( $aimDir );
  129. while ( false !== ($file = readdir ( $dirHandle )) ) {
  130. if ($file == '.' || $file == '..') {
  131. continue;
  132. }
  133. if (! is_dir ( $aimDir . $file )) {
  134. $this->unlinkFile ( $aimDir . $file );
  135. } else {
  136. $this->unlinkDir ( $aimDir . $file );
  137. }
  138. }
  139. closedir ( $dirHandle );
  140. return rmdir ( $aimDir );
  141. }
  142. /**
  143. * 刪除檔案
  144. *
  145. * @param string $aimUrl
  146. * @return boolean
  147. */
  148. function unlinkFile($aimUrl) {
  149. if (file_exists ( $aimUrl )) {
  150. unlink ( $aimUrl );
  151. return true;
  152. } else {
  153. return false;
  154. }
  155. }
  156. /**
  157. * 複製檔案夾
  158. *
  159. * @param string $oldDir
  160. * @param string $aimDir
  161. * @param boolean $overWrite 該參數控制是否覆蓋原檔案
  162. * @return boolean
  163. */
  164. function copyDir($oldDir, $aimDir, $overWrite = false) {
  165. $aimDir = str_replace ( '', '/', $aimDir );
  166. $aimDir = substr ( $aimDir, - 1 ) == '/' ? $aimDir : $aimDir . '/';
  167. $oldDir = str_replace ( '', '/', $oldDir );
  168. $oldDir = substr ( $oldDir, - 1 ) == '/' ? $oldDir : $oldDir . '/';
  169. if (! is_dir ( $oldDir )) {
  170. return false;
  171. }
  172. if (! file_exists ( $aimDir )) {
  173. $this->createDir ( $aimDir );
  174. }
  175. $dirHandle = opendir ( $oldDir );
  176. while ( false !== ($file = readdir ( $dirHandle )) ) {
  177. if ($file == '.' || $file == '..') {
  178. continue;
  179. }
  180. if (! is_dir ( $oldDir . $file )) {
  181. $this->copyFile ( $oldDir . $file, $aimDir . $file, $overWrite );
  182. } else {
  183. $this->copyDir ( $oldDir . $file, $aimDir . $file, $overWrite );
  184. }
  185. }
  186. return closedir ( $dirHandle );
  187. }
  188. /**
  189. * 複製檔案
  190. *
  191. * @param string $fileUrl
  192. * @param string $aimUrl
  193. * @param boolean $overWrite 該參數控制是否覆蓋原檔案
  194. * @return boolean
  195. */
  196. function copyFile($fileUrl, $aimUrl, $overWrite = false) {
  197. if (! file_exists ( $fileUrl )) {
  198. return false;
  199. }
  200. if (file_exists ( $aimUrl ) && $overWrite == false) {
  201. return false;
  202. } elseif (file_exists ( $aimUrl ) && $overWrite == true) {
  203. $this->unlinkFile ( $aimUrl );
  204. }
  205. $aimDir = dirname ( $aimUrl );
  206. $this->createDir ( $aimDir );
  207. copy ( $fileUrl, $aimUrl );
  208. return true;
  209. }
  210. /**
  211. * 將字串寫入檔案
  212. *
  213. * @param string $filename 檔案名稱
  214. * @param boolean $str 待寫入的字元資料
  215. */
  216. function writeFile($filename, $str) {
  217. if (function_exists ( file_put_contents )) {
  218. file_put_contents ( $filename, $str );
  219. } else {
  220. $fp = fopen ( $filename, "wb" );
  221. fwrite ( $fp, $str );
  222. fclose ( $fp );
  223. }
  224. }
  225. /**
  226. * 將整個檔案內容讀出到一個字串中
  227. *
  228. * @param string $filename 檔案名稱
  229. * @return array
  230. */
  231. function readsFile($filename) {
  232. if (function_exists ( file_get_contents )) {
  233. return file_get_contents ( $filename );
  234. } else {
  235. $fp = fopen ( $filename, "rb" );
  236. $str = fread ( $fp, filesize ( $filename ) );
  237. fclose ( $fp );
  238. return $str;
  239. }
  240. }
  241. /**
  242. * 將檔案內容讀出到一個數組中
  243. *
  244. * @param string $filename 檔案名稱
  245. * @return array
  246. */
  247. function readFile2array($filename) {
  248. $file = file ( $filename );
  249. $arr = array ();
  250. foreach ( $file as $value ) {
  251. $arr [] = trim ( $value );
  252. }
  253. return $arr;
  254. }
  255. /**
  256. * 轉化 \ 為 /
  257. *
  258. * @param string $path 路徑
  259. * @return string 路徑
  260. */
  261. function dirPath($path) {
  262. $path = str_replace ( '\\', '/', $path );
  263. if (substr ( $path, - 1 ) != '/')
  264. $path = $path . '/';
  265. return $path;
  266. }
  267. /**
  268. * 轉換目錄下面的所有檔案編碼格式
  269. *
  270. * @param string $in_charset 原字元集
  271. * @param string $out_charset 目標字元集
  272. * @param string $dir 目錄位址
  273. * @param string $fileexts 轉換的檔案格式
  274. * @return string 如果原字元集和目標字元集相同則返回false,否則為true
  275. */
  276. function dirIconv($in_charset, $out_charset, $dir, $fileexts = 'php|html|htm|shtml|shtm|js|txt|xml') {
  277. if ($in_charset == $out_charset)
  278. return false;
  279. $list = $this->dirList ( $dir );
  280. foreach ( $list as $v ) {
  281. if (preg_match ( "/\.($fileexts)/i", $v ) && is_file ( $v )) {
  282. file_put_contents ( $v, iconv ( $in_charset, $out_charset, file_get_contents ( $v ) ) );
  283. }
  284. }
  285. return true;
  286. }
  287. /**
  288. * 列出目錄下所有檔案
  289. *
  290. * @param string $path 路徑
  291. * @param string $exts 副檔名
  292. * @param array $list 增加的檔案清單
  293. * @return array 所有滿足條件的檔案
  294. */
  295. function dirList($path, $exts = '', $list = array()) {
  296. $path = $this->dirPath ( $path );
  297. $files = glob ( $path . '*' );
  298. foreach ( $files as $v ) {
  299. $fileext = $this->fileext ( $v );
  300. if (! $exts || preg_match ( "/\.($exts)/i", $v )) {
  301. $list [] = $v;
  302. if (is_dir ( $v )) {
  303. $list = $this->dirList ( $v, $exts, $list );
  304. }
  305. }
  306. }
  307. return $list;
  308. }
  309. /**
  310. * 設定目錄下面的所有檔案的訪問和修改時間
  311. *
  312. * @param string $path 路徑
  313. * @param int $mtime 修改時間
  314. * @param int $atime 訪問時間
  315. * @return array 不是目錄時返回false,否則返回 true
  316. */
  317. function dirTouch($path, $mtime = TIME, $atime = TIME) {
  318. if (! is_dir ( $path ))
  319. return false;
  320. $path = $this->dirPath ( $path );
  321. if (! is_dir ( $path ))
  322. touch ( $path, $mtime, $atime );
  323. $files = glob ( $path . '*' );
  324. foreach ( $files as $v ) {
  325. is_dir ( $v ) ? $this->dirTouch ( $v, $mtime, $atime ) : touch ( $v, $mtime, $atime );
  326. }
  327. return true;
  328. }
  329. /**
  330. * 目錄列表
  331. *
  332. * @param string $dir 路徑
  333. * @param int $parentid 父id
  334. * @param array $dirs 傳入的目錄
  335. * @return array 返回目錄及子目錄列表
  336. */
  337. function dirTree($dir, $parentid = 0, $dirs = array()) {
  338. global $id;
  339. if ($parentid == 0)
  340. $id = 0;
  341. $list = glob ( $dir . '*' );
  342. foreach ( $list as $v ) {
  343. if (is_dir ( $v )) {
  344. $id ++;
  345. $dirs [$id] = array ('id' => $id, 'parentid' => $parentid, 'name' => basename ( $v ), 'dir' => $v . '/' );
  346. $dirs = $this->dirTree ( $v . '/', $id, $dirs );
  347. }
  348. }
  349. return $dirs;
  350. }
  351. /**
  352. * 目錄列表
  353. *
  354. * @param string $dir 路徑
  355. * @return array 返回目錄列表
  356. */
  357. function dirNodeTree($dir) {
  358. $d = dir ( $dir );
  359. $dirs = array();
  360. while ( false !== ($entry = $d->read ()) ) {
  361. if ($entry != '.' and $entry != '..' and is_dir ( $dir . '/' . $entry )) {
  362. $dirs[] = $entry;
  363. }
  364. }
  365. return $dirs;
  366. }
  367. /**
  368. * 擷取目錄大小
  369. *
  370. * @param string $dirname 目錄
  371. * @return string 位元B
  372. */
  373. function getDirSize($dirname) {
  374. if (! file_exists ( $dirname ) or ! is_dir ( $dirname ))
  375. return false;
  376. if (! $handle = opendir ( $dirname ))
  377. return false;
  378. $size = 0;
  379. while ( false !== ($file = readdir ( $handle )) ) {
  380. if ($file == "." or $file == "..")
  381. continue;
  382. $file = $dirname . "/" . $file;
  383. if (is_dir ( $file )) {
  384. $size += $this->getDirSize ( $file );
  385. } else {
  386. $size += filesize ( $file );
  387. }
  388. }
  389. closedir ( $handle );
  390. return $size;
  391. }
  392. /**
  393. * 將位元組轉換成Kb或者Mb...
  394. * 參數 $size為位元組大小
  395. */
  396. function bitSize($size) {
  397. if (! preg_match ( "/^[0-9]+$/", $num ))
  398. return 0;
  399. $type = array ("B", "KB", "MB", "GB", "TB", "PB" );
  400. $j = 0;
  401. while ( $num >= 1024 ) {
  402. if ($j >= 5)
  403. return $num . $type [$j];
  404. $num = $num / 1024;
  405. $j ++;
  406. }
  407. return $num . $type [$j];
  408. }
  409. /**
  410. * 擷取檔案名稱尾碼
  411. *
  412. * @param string $filename
  413. * @return string
  414. */
  415. function fileext($filename) {
  416. return addslashes ( trim ( substr ( strrchr ( $filename, '.' ), 1, 10 ) ) );
  417. }
  418. }
  419. ?>
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.