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