1: <?php namespace Laravel; use FilesystemIterator as fIterator;
2:
3: class File {
4:
5: /**
6: * Determine if a file exists.
7: *
8: * @param string $path
9: * @return bool
10: */
11: public static function exists($path)
12: {
13: return file_exists($path);
14: }
15:
16: /**
17: * Get the contents of a file.
18: *
19: * <code>
20: * // Get the contents of a file
21: * $contents = File::get(path('app').'routes'.EXT);
22: *
23: * // Get the contents of a file or return a default value if it doesn't exist
24: * $contents = File::get(path('app').'routes'.EXT, 'Default Value');
25: * </code>
26: *
27: * @param string $path
28: * @param mixed $default
29: * @return string
30: */
31: public static function get($path, $default = null)
32: {
33: return (file_exists($path)) ? file_get_contents($path) : value($default);
34: }
35:
36: /**
37: * Write to a file.
38: *
39: * @param string $path
40: * @param string $data
41: * @return int
42: */
43: public static function put($path, $data)
44: {
45: return file_put_contents($path, $data, LOCK_EX);
46: }
47:
48: /**
49: * Append to a file.
50: *
51: * @param string $path
52: * @param string $data
53: * @return int
54: */
55: public static function append($path, $data)
56: {
57: return file_put_contents($path, $data, LOCK_EX | FILE_APPEND);
58: }
59:
60: /**
61: * Delete a file.
62: *
63: * @param string $path
64: * @return bool
65: */
66: public static function delete($path)
67: {
68: if (static::exists($path)) return @unlink($path);
69: }
70:
71: /**
72: * Move a file to a new location.
73: *
74: * @param string $path
75: * @param string $target
76: * @return void
77: */
78: public static function move($path, $target)
79: {
80: return rename($path, $target);
81: }
82:
83: /**
84: * Copy a file to a new location.
85: *
86: * @param string $path
87: * @param string $target
88: * @return void
89: */
90: public static function copy($path, $target)
91: {
92: return copy($path, $target);
93: }
94:
95: /**
96: * Extract the file extension from a file path.
97: *
98: * @param string $path
99: * @return string
100: */
101: public static function extension($path)
102: {
103: return pathinfo($path, PATHINFO_EXTENSION);
104: }
105:
106: /**
107: * Get the file type of a given file.
108: *
109: * @param string $path
110: * @return string
111: */
112: public static function type($path)
113: {
114: return filetype($path);
115: }
116:
117: /**
118: * Get the file size of a given file.
119: *
120: * @param string $path
121: * @return int
122: */
123: public static function size($path)
124: {
125: return filesize($path);
126: }
127:
128: /**
129: * Get the file's last modification time.
130: *
131: * @param string $path
132: * @return int
133: */
134: public static function modified($path)
135: {
136: return filemtime($path);
137: }
138:
139: /**
140: * Get a file MIME type by extension.
141: *
142: * <code>
143: * // Determine the MIME type for the .tar extension
144: * $mime = File::mime('tar');
145: *
146: * // Return a default value if the MIME can't be determined
147: * $mime = File::mime('ext', 'application/octet-stream');
148: * </code>
149: *
150: * @param string $extension
151: * @param string $default
152: * @return string
153: */
154: public static function mime($extension, $default = 'application/octet-stream')
155: {
156: $mimes = Config::get('mimes');
157:
158: if ( ! array_key_exists($extension, $mimes)) return $default;
159:
160: return (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
161: }
162:
163: /**
164: * Determine if a file is of a given type.
165: *
166: * The Fileinfo PHP extension is used to determine the file's MIME type.
167: *
168: * <code>
169: * // Determine if a file is a JPG image
170: * $jpg = File::is('jpg', 'path/to/file.jpg');
171: *
172: * // Determine if a file is one of a given list of types
173: * $image = File::is(array('jpg', 'png', 'gif'), 'path/to/file');
174: * </code>
175: *
176: * @param array|string $extensions
177: * @param string $path
178: * @return bool
179: */
180: public static function is($extensions, $path)
181: {
182: $mimes = Config::get('mimes');
183:
184: $mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path);
185:
186: // The MIME configuration file contains an array of file extensions and
187: // their associated MIME types. We will loop through each extension the
188: // developer wants to check and look for the MIME type.
189: foreach ((array) $extensions as $extension)
190: {
191: if (isset($mimes[$extension]) and in_array($mime, (array) $mimes[$extension]))
192: {
193: return true;
194: }
195: }
196:
197: return false;
198: }
199:
200: /**
201: * Create a new directory.
202: *
203: * @param string $path
204: * @param int $chmod
205: * @return void
206: */
207: public static function mkdir($path, $chmod = 0777)
208: {
209: return ( ! is_dir($path)) ? mkdir($path, $chmod, true) : true;
210: }
211:
212: /**
213: * Move a directory from one location to another.
214: *
215: * @param string $source
216: * @param string $destination
217: * @param int $options
218: * @return void
219: */
220: public static function mvdir($source, $destination, $options = fIterator::SKIP_DOTS)
221: {
222: return static::cpdir($source, $destination, true, $options);
223: }
224:
225: /**
226: * Recursively copy directory contents to another directory.
227: *
228: * @param string $source
229: * @param string $destination
230: * @param bool $delete
231: * @param int $options
232: * @return void
233: */
234: public static function cpdir($source, $destination, $delete = false, $options = fIterator::SKIP_DOTS)
235: {
236: if ( ! is_dir($source)) return false;
237:
238: // First we need to create the destination directory if it doesn't
239: // already exists. This directory hosts all of the assets we copy
240: // from the installed bundle's source directory.
241: if ( ! is_dir($destination))
242: {
243: mkdir($destination, 0777, true);
244: }
245:
246: $items = new fIterator($source, $options);
247:
248: foreach ($items as $item)
249: {
250: $location = $destination.DS.$item->getBasename();
251:
252: // If the file system item is a directory, we will recurse the
253: // function, passing in the item directory. To get the proper
254: // destination path, we'll add the basename of the source to
255: // to the destination directory.
256: if ($item->isDir())
257: {
258: $path = $item->getRealPath();
259:
260: if (! static::cpdir($path, $location, $delete, $options)) return false;
261:
262: if ($delete) @rmdir($item->getRealPath());
263: }
264: // If the file system item is an actual file, we can copy the
265: // file from the bundle asset directory to the public asset
266: // directory. The "copy" method will overwrite any existing
267: // files with the same name.
268: else
269: {
270: if(! copy($item->getRealPath(), $location)) return false;
271:
272: if ($delete) @unlink($item->getRealPath());
273: }
274: }
275:
276: unset($items);
277: if ($delete) @rmdir($source);
278:
279: return true;
280: }
281:
282: /**
283: * Recursively delete a directory.
284: *
285: * @param string $directory
286: * @param bool $preserve
287: * @return void
288: */
289: public static function rmdir($directory, $preserve = false)
290: {
291: if ( ! is_dir($directory)) return;
292:
293: $items = new fIterator($directory);
294:
295: foreach ($items as $item)
296: {
297: // If the item is a directory, we can just recurse into the
298: // function and delete that sub-directory, otherwise we'll
299: // just delete the file and keep going!
300: if ($item->isDir())
301: {
302: static::rmdir($item->getRealPath());
303: }
304: else
305: {
306: @unlink($item->getRealPath());
307: }
308: }
309:
310: unset($items);
311: if ( ! $preserve) @rmdir($directory);
312: }
313:
314: /**
315: * Empty the specified directory of all files and folders.
316: *
317: * @param string $directory
318: * @return void
319: */
320: public static function cleandir($directory)
321: {
322: return static::rmdir($directory, true);
323: }
324:
325: /**
326: * Get the most recently modified file in a directory.
327: *
328: * @param string $directory
329: * @param int $options
330: * @return SplFileInfo
331: */
332: public static function latest($directory, $options = fIterator::SKIP_DOTS)
333: {
334: $latest = null;
335:
336: $time = 0;
337:
338: $items = new fIterator($directory, $options);
339:
340: // To get the latest created file, we'll simply loop through the
341: // directory, setting the latest file if we encounter a file
342: // with a UNIX timestamp greater than the latest one.
343: foreach ($items as $item)
344: {
345: if ($item->getMTime() > $time)
346: {
347: $latest = $item;
348: $time = $item->getMTime();
349: }
350: }
351:
352: return $latest;
353: }
354:
355: }