1: <?php
2: /**
3: * @filesource Kotchasan/File.php
4: *
5: * @copyright 2016 Goragod.com
6: * @license http://www.kotchasan.com/license/
7: *
8: * @see http://www.kotchasan.com/
9: */
10:
11: namespace Kotchasan;
12:
13: /**
14: * คลาสสำหรับจัดการไฟล์และไดเร็คทอรี่.
15: *
16: * @author Goragod Wiriya <admin@goragod.com>
17: *
18: * @since 1.0
19: */
20: class File
21: {
22: /**
23: * สำเนาไดเร็คทอรี่.
24: *
25: * @param string $dir ไดเร็คทอรี่ต้นทาง มี / ปิดท้ายด้วย
26: * @param string $todir ไดเร็คทอรี่ปลายทาง มี / ปิดท้ายด้วย
27: */
28: public static function copyDirectory($dir, $todir)
29: {
30: $f = opendir($dir);
31: while (false !== ($text = readdir($f))) {
32: if ($text !== '.' && $text !== '..') {
33: if (is_dir($dir.$text)) {
34: self::makeDirectory($todir.$text.'/');
35: self::copyDirectory($dir.$text.'/', $todir.$text.'/');
36: } elseif (is_dir($todir)) {
37: copy($dir.$text, $todir.$text);
38: }
39: }
40: }
41: closedir($f);
42: }
43:
44: /**
45: * อ่านนามสกุลของไฟล์เช่น config.php คืนค่า php
46: * คืนค่า ext ของไฟล์ ตัวอักษรตัวพิมพ์เล็ก
47: *
48: * @assert ('index.php.sql') [==] 'sql'
49: *
50: * @param string $path ไฟล์
51: *
52: * @return string
53: */
54: public static function ext($path)
55: {
56: $exts = explode('.', strtolower($path));
57:
58: return end($exts);
59: }
60:
61: /**
62: * อ่านรายชื่อไฟล์ภายใต้ไดเร็คทอรี่รวมไดเร็คทอรี่ย่อย.
63: *
64: * @param string $dir ไดเร็คทอรี่ มี / ปิดท้ายด้วย
65: * @param array $result คืนค่ารายการไฟล์ที่พบ
66: * @param array $filter (option) ไฟล์ฟิลเตอร์ ตัวพิมพ์เล็ก เช่น array('jpg','gif') แอเรย์ว่างหมายถึงทุกนามสกุล
67: */
68: public static function listFiles($dir, &$result, $filter = array())
69: {
70: if (is_dir($dir)) {
71: $f = opendir($dir);
72: if ($f) {
73: while (false !== ($text = readdir($f))) {
74: if ($text !== '.' && $text !== '..') {
75: if (is_dir($dir.$text)) {
76: self::listFiles($dir.$text.'/', $result, $filter);
77: } elseif (empty($filter) || in_array(self::ext($text), $filter)) {
78: $result[] = $dir.$text;
79: }
80: }
81: }
82: closedir($f);
83: }
84: }
85: }
86:
87: /**
88: * สร้างและตรวจสอบไดเร็คทอรี่ ให้เขียนได้.
89: *
90: * @param string $dir
91: * @param int $mode (optional) default 0755
92: *
93: * @return bool
94: */
95: public static function makeDirectory($dir, $mode = 0755)
96: {
97: if (!is_dir($dir)) {
98: $oldumask = umask(0);
99: mkdir($dir, $mode);
100: umask($oldumask);
101: }
102: if (!is_writable($dir)) {
103: $oldumask = umask(0);
104: $f = @chmod($dir, $mode);
105: umask($oldumask);
106:
107: return $f;
108: }
109:
110: return true;
111: }
112:
113: /**
114: * ลบไดเรคทอรี่และไฟล์ หรือ ไดเร็คทอรี่ในนั้นทั้งหมด.
115: *
116: * @param string $dir ไดเรคทอรี่ที่ต้องการลบ มี / ต่อท้ายด้วย
117: */
118: public static function removeDirectory($dir)
119: {
120: if (is_dir($dir)) {
121: $f = opendir($dir);
122: while (false !== ($text = readdir($f))) {
123: if ($text != '.' && $text != '..') {
124: if (is_dir($dir.$text)) {
125: self::removeDirectory($dir.$text.'/');
126: } else {
127: @unlink($dir.$text);
128: }
129: }
130: }
131: closedir($f);
132: @rmdir($dir);
133: }
134: }
135: }
136: