120 lines
2.3 KiB
PHP
Executable File
120 lines
2.3 KiB
PHP
Executable File
<?php
|
|
|
|
class FileSystem {
|
|
|
|
|
|
public static function chmod ( $file, $mode = 0 ) {
|
|
|
|
if ( !file_exists($file) )
|
|
return false;
|
|
|
|
$chmod = chmod($file, $mode);
|
|
|
|
if ( !is_dir($file) )
|
|
return $chmod;
|
|
|
|
$files = new RecursiveIteratorIterator(
|
|
new RecursiveDirectoryIterator(
|
|
$file,
|
|
RecursiveDirectoryIterator::SKIP_DOTS
|
|
),
|
|
RecursiveIteratorIterator::CHILD_FIRST
|
|
);
|
|
|
|
foreach ($files as $fileinfo) {
|
|
|
|
$path = $fileinfo->getRealPath();
|
|
$chmod &= chmod($path, $mode);
|
|
|
|
}
|
|
|
|
return $chmod;
|
|
|
|
}
|
|
|
|
public static function rm ( $file ) {
|
|
|
|
$rm = 1;
|
|
if ( file_exists($file) ) {
|
|
|
|
if ( is_dir($file) ) {
|
|
|
|
$files = new RecursiveIteratorIterator(
|
|
new RecursiveDirectoryIterator(
|
|
$file,
|
|
RecursiveDirectoryIterator::SKIP_DOTS
|
|
),
|
|
RecursiveIteratorIterator::CHILD_FIRST
|
|
);
|
|
|
|
foreach ($files as $fileinfo) {
|
|
|
|
$path = $fileinfo->getRealPath();
|
|
|
|
if ( $fileinfo->isDir() )
|
|
$rm &= rmdir($path);
|
|
|
|
else $rm &= unlink($path);
|
|
|
|
}
|
|
|
|
$rm &= rmdir($file);
|
|
|
|
} else $rm &= unlink($file);
|
|
|
|
} else return false;
|
|
|
|
return $rm;
|
|
|
|
}
|
|
|
|
public static function zip ( $archive, $file, $exclude = null ) {
|
|
|
|
if ( file_exists($file) ) {
|
|
|
|
$zip = new ZipArchive();
|
|
$zip->open($archive, ZIPARCHIVE::CREATE);
|
|
|
|
if ( $exclude )
|
|
$exclude = Path::get(realpath($exclude));
|
|
|
|
if ( is_dir($file) ) {
|
|
|
|
$files = new RecursiveIteratorIterator(
|
|
new RecursiveCallbackFilterIterator(
|
|
new RecursiveDirectoryIterator(
|
|
$file,
|
|
RecursiveDirectoryIterator::SKIP_DOTS
|
|
),
|
|
$exclude ?
|
|
function ( $current, $key, $iterator) use ( $exclude ) {
|
|
return !Path::get($current->getRealPath())->shift($exclude);
|
|
} :
|
|
function ( $current, $key, $iterator) { return true; }
|
|
),
|
|
RecursiveIteratorIterator::SELF_FIRST
|
|
);
|
|
|
|
$root = Path::get(realpath($file));
|
|
foreach ($files as $fileinfo) {
|
|
|
|
$path = Path::get($fileinfo->getRealPath())->shift($root);
|
|
|
|
if ( $fileinfo->isDir() )
|
|
$zip->addEmptyDir($path);
|
|
|
|
else $zip->addFile($fileinfo->getRealPath(), $path);
|
|
|
|
}
|
|
|
|
} else $zip->addFile($file);
|
|
|
|
$zip->close();
|
|
|
|
} else throw new Exception(sprintf('File "%1$s" does not exists !', $file));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|