10 const FIND_FILTER_TYPE =
'type';
11 const FIND_FILTER_NAME =
'name';
12 const FIND_FILTER_TIME =
'time';
13 const FIND_FILTER_SIZE =
'size';
14 const FIND_FILTER_FUNC =
'func';
17 const FIND_TYPE_DIR = 1;
18 const FIND_TYPE_FILE = 2;
19 const FIND_TYPE_ALL = 255;
21 const EXT_MASK =
'/\\.([^\\.\\/\\\\]*)$/';
29 return preg_match(
'/[\\\\\\/]$/',$path) ? $path : $path . DIRECTORY_SEPARATOR;
36 public static function windows($filename){
37 return strtolower(str_replace(
'/',
'\\',$filename));
45 public static function inDir($path,$filename){
46 $path = self::addDirSeparator($path);
48 $path = self::windows($path);
49 $filename = self::windows($filename);
51 return strpos($path,$filename) === 0;
59 if(!($paths = ini_get(
'open_basedir')))
return true;
60 foreach(explode(PATH_SEPARATOR,$paths) as $path)
if(self::inDir($path,$filename))
return true;
69 public static function mkdir($path,$mode = 0777){
71 $result = is_dir($path) || mkdir($path,$mode,
true);
80 public static function rmdir($path){
81 if(!is_dir($path))
return true;
82 foreach((
new \FilesystemIterator($path,\FilesystemIterator::SKIP_DOTS)) as $filename => $file)
83 if($file->isDir()) self::rmdir($filename);
84 else unlink($filename);
93 public static function dirname($path,$levels = 1){
94 while($levels-- && $path) $path = substr($path,0,max(strrpos($path,
'/'),strrpos($path,
'\\')));
103 return preg_replace(
'/(^.*[\\\\\\/]|\\?.*$)/',
'',$path);
111 public static function ext($filename,$lower_case =
true){
112 return preg_match(self::EXT_MASK,$filename,$match) ? ($lower_case ? strtolower($match[1]) : $match[1]) : null;
121 return preg_replace(self::EXT_MASK,
'',$filename) . $ext;
128 $path = sys_get_temp_dir();
130 $path = dirname($filename = tempnam(__FILE__,
''));
133 return self::addDirSeparator($path);
141 public static function tempFile($ext =
'tmp',$data = null){
142 $dir = self::tempDir();
143 while(file_exists($filename = $dir . Str::random() .
'.' . $ext));
144 file_put_contents($filename,$data);
152 public static function mtime($filename){
154 return filemtime($filename);
156 catch(\Exception $e){
165 public static function unlink($filename){
167 return unlink($filename);
169 catch(\Exception $e){
170 return !file_exists($filename);
181 public static function write($filename,$data,$mode = null,$append =
false){
182 $result = file_put_contents($filename,$data,$append ? FILE_APPEND : null);
183 if($mode) @chmod($filename,$mode);
192 public static function read($filename,$default =
false){
193 return is_file($filename) ? file_get_contents($filename) : $default;
202 public static function serialize($filename,$data,$mode = null){
203 return self::write($filename,serialize($data),$mode);
212 return is_file($filename) ? unserialize(file_get_contents($filename)) : $default;
222 public static function jsonEncode($filename,$data,$mode = null,$options = JSON_PRETTY_PRINT){
223 return self::write($filename,json_encode($data,$options),$mode);
232 public static function jsonDecode($filename,$default =
false,$assoc =
true){
233 return is_file($filename) ? json_decode(file_get_contents($filename),$assoc) : $default;
240 public static function mime($filename){
241 if(function_exists(
'mime_content_type()') && is_file($filename))
return mime_content_type($filename);
242 if(array_key_exists($ext = self::ext($filename),$types = require(__DIR__ .
'/../../data/mime.php')))
return $types[$ext];
243 return 'application/' . $ext;
254 public static function find($path,$filters = null,$recursive =
false){
255 if(!is_dir($path))
return false;
256 if(!$filters) $filters = [];
257 if(!array_key_exists(self::FIND_FILTER_TYPE,$filters)) $filters[self::FIND_FILTER_TYPE] = self::FIND_TYPE_FILE;
259 $dir = dir($path = self::addDirSeparator($path));
260 while(($entry = $dir->read()) !==
false)
if(trim($entry,
'.') !==
'')
try{
261 $info = [
'dir' => $is_dir = is_dir($full = $path . $entry)];
262 if($is_dir && $recursive) $files += self::find($full,$filters,$recursive);
263 foreach($filters as $key => $value){
264 if($operator = preg_match(
'/^(\\w+)(\\W+)$/',$key,$match) ? $match[2] : null) $key = $match[1];
265 else $operator =
'==';
267 case self::FIND_FILTER_TYPE:
268 if($value == self::FIND_TYPE_ALL)
break;
269 if(($value & self::FIND_TYPE_DIR) && !$is_dir)
continue 3;
270 if(($value & self::FIND_TYPE_FILE) && $is_dir)
continue 3;
272 case self::FIND_FILTER_NAME:
273 if(!Str::operator($info[$key] = $entry,$operator,$value))
continue 3;
275 case self::FIND_FILTER_TIME:
276 if($is_dir || !Str::operator($info[$key] = filemtime($full),$operator,$value))
continue 3;
278 case self::FIND_FILTER_SIZE:
279 if($is_dir || !Str::operator($info[$key] = filesize($full),$operator,\
Rsi\Number::shorthandBytes($value)))
continue 3;
281 case self::FIND_FILTER_FUNC:
282 if(!($info[$key] = call_user_func($value,$full,$info)))
continue 3;
286 $files[$full] = $info;
288 catch(\Exception $e){}
299 public static function dir($path,$pattern = null,$recursive =
false){
300 $filters = [self::FIND_FILTER_TYPE => self::FIND_TYPE_FILE];
301 if($pattern) $filters[self::FIND_FILTER_NAME .
'//'] =
'/^' . strtr(preg_quote($pattern,
'/') .
'$/i',[
'\\*' =>
'.*',
'\\?' =>
'.']);
302 return ($files = self::find($path,$filters,$recursive)) ? array_keys($files) : $files;
static write($filename, $data, $mode=null, $append=false)
Write data to a file.
static inDir($path, $filename)
Check if a filename is within a directory.
static jsonDecode($filename, $default=false, $assoc=true)
Decode the JSON content of a file.
static ext($filename, $lower_case=true)
Get extension of a filename.
static rmdir($path)
Recursivly remove a directory, including all files.
static mtime($filename)
Optimistic filemtime (no need to check file_exists() first).
File and file system helper functions.
static windows($filename)
Normalize a Windows filename.
static jsonEncode($filename, $data, $mode=null, $options=JSON_PRETTY_PRINT)
Encode data to JSON and write it to a file.
static windows()
Check wether we are running on a Windows machine.
static inBaseDir($filename)
Check if a filename is within the base dir restriction.
static tempFile($ext='tmp', $data=null)
Create a temporarily file.
static tempDir()
Temporarily directory of the system.
static dirname($path, $levels=1)
Dirname that does not care about separator, and does not return '.
static addDirSeparator($path)
Add a directory separator to the path (if not present)
static basename($path)
Basename that does not care about separator, and also removes possible query parameters.
static dir($path, $pattern=null, $recursive=false)
Find files.
static changeExt($filename, $ext)
Change the extension of a filename.
static read($filename, $default=false)
Read the content of a file.
static mkdir($path, $mode=0777)
Create a directory (if not exists).
static serialize($filename, $data, $mode=null)
Serialize data and write it to a file.
static find($path, $filters=null, $recursive=false)
Find and filter files.
static mime($filename)
Get MIME type for a file.
static unlink($filename)
Safe unlink.
static unserialize($filename, $default=false)
Unserialize the content of a file.