bludit/bl-kernel/helpers/filesystem.class.php

137 lines
3.1 KiB
PHP
Raw Normal View History

2015-05-05 03:00:01 +02:00
<?php defined('BLUDIT') or die('Bludit CMS.');
2015-03-08 18:02:59 +01:00
2015-05-31 03:06:55 +02:00
class Filesystem {
2015-03-08 18:02:59 +01:00
2015-09-20 23:46:50 +02:00
// Returns an array with the absolutes directories.
2017-06-26 22:50:56 +02:00
public static function listDirectories($path, $regex='*', $sortByDate=false)
2015-03-08 18:02:59 +01:00
{
2015-09-15 01:07:15 +02:00
$directories = glob($path.$regex, GLOB_ONLYDIR);
if(empty($directories)) {
return array();
}
2017-06-26 22:50:56 +02:00
if($sortByDate) {
usort($directories, create_function('$a,$b', 'return filemtime($b) - filemtime($a);'));
}
2015-09-15 01:07:15 +02:00
return $directories;
}
2015-11-04 01:28:11 +01:00
public static function listFiles($path, $regex='*', $extension='*', $sortByDate=false)
2015-09-15 01:07:15 +02:00
{
$files = glob($path.$regex.'.'.$extension);
if(empty($files)) {
return array();
}
2015-11-04 01:28:11 +01:00
if($sortByDate) {
usort($files, create_function('$a,$b', 'return filemtime($b) - filemtime($a);'));
}
2015-09-15 01:07:15 +02:00
return $files;
2015-03-08 18:02:59 +01:00
}
2015-05-05 03:00:01 +02:00
public static function mkdir($pathname, $recursive=false)
{
// DEBUG: Ver permisos si son correctos
return mkdir($pathname, 0755, $recursive);
}
public static function rmdir($pathname)
{
return rmdir($pathname);
}
public static function mv($oldname, $newname)
{
return rename($oldname, $newname);
}
public static function rmfile($filename)
{
return unlink($filename);
}
2017-06-29 22:13:25 +02:00
public static function fileExists($filename)
{
return file_exists($filename);
}
2017-07-02 18:55:27 +02:00
public static function directoryExists($path)
{
return file_exists($path);
}
public static function copyRecursive($source, $destination)
{
2017-10-02 23:17:32 +02:00
if (!self::directoryExists($source)) {
return false;
}
2017-07-02 18:55:27 +02:00
$destination = rtrim($destination, '/');
foreach($iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST) as $item) {
if($item->isDir()) {
@mkdir($destination.DS.$iterator->getSubPathName());
} else {
copy($item, $destination.DS.$iterator->getSubPathName());
}
}
return true;
}
public static function deleteRecursive($source)
{
2017-10-02 23:17:32 +02:00
if (!self::directoryExists($source)) {
return false;
}
2017-07-02 18:55:27 +02:00
foreach(new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($source, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST) as $item) {
if($item->isFile()) {
unlink($item);
} else {
rmdir($item);
}
}
return rmdir($source);
}
public static function zip($source, $destination)
{
if (!extension_loaded('zip') || !file_exists($source)) {
return false;
}
$zip = new ZipArchive();
if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
return false;
}
if (is_dir($source) === true) {
$iterator = new RecursiveDirectoryIterator($source);
$iterator->setFlags(RecursiveDirectoryIterator::SKIP_DOTS);
$files = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file) {
$file = realpath($file);
if (is_dir($file)) {
$zip->addEmptyDir(str_replace($source, '', $file));
} elseif (is_file($file)) {
$zip->addFromString(str_replace($source, '', $file), file_get_contents($file));
}
}
} elseif (is_file($source)) {
$zip->addFromString(basename($source), file_get_contents($source));
}
return $zip->close();
}
2016-01-12 03:18:20 +01:00
}