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

226 lines
5.5 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) {
2017-12-26 17:45:02 +01:00
usort($directories,
2017-12-06 19:14:33 +01:00
function($a, $b) {
return filemtime($b) - filemtime($a);
}
);
2017-06-26 22:50:56 +02:00
}
2015-09-15 01:07:15 +02:00
return $directories;
}
2017-12-26 17:45:02 +01:00
// Returns an array with the list of files with the absolute path
// $sortByDate = TRUE, the first file is the newer file
// $chunk = amount of chunks, FALSE if you don't want to chunk
public static function listFiles($path, $regex='*', $extension='*', $sortByDate=false, $chunk=false)
2015-09-15 01:07:15 +02:00
{
$files = glob($path.$regex.'.'.$extension);
2017-11-16 23:22:55 +01:00
if (empty($files)) {
2015-09-15 01:07:15 +02:00
return array();
}
2017-11-16 23:22:55 +01:00
if ($sortByDate) {
2017-12-26 17:45:02 +01:00
usort($files,
2017-12-06 19:14:33 +01:00
function($a, $b) {
return filemtime($b) - filemtime($a);
}
);
2015-11-04 01:28:11 +01:00
}
// Split the list of files into chunks
// http://php.net/manual/en/function.array-chunk.php
if ($chunk) {
return array_chunk($files, $chunk);
}
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);
}
2017-11-16 23:22:55 +01:00
// Copy recursive a directory to another
// If the destination directory not exists is created
// $source = /home/diego/example or /home/diego/example/
// $destination = /home/diego/newplace or /home/diego/newplace/
2018-03-07 15:43:41 +01:00
public static function copyRecursive($source, $destination, $skipDirectory=false)
2017-07-02 18:55:27 +02:00
{
2017-11-16 23:22:55 +01:00
$source = rtrim($source, DS);
$destination = rtrim($destination, DS);
// Check $source directory if exists
2017-10-02 23:17:32 +02:00
if (!self::directoryExists($source)) {
return false;
}
2017-11-16 23:22:55 +01:00
// Check $destionation directory if exists
if (!self::directoryExists($destination)) {
// Create the $destination directory
if (!mkdir($destination, 0755, true)) {
return false;
}
}
2017-07-02 18:55:27 +02:00
2017-11-16 23:22:55 +01:00
foreach ($iterator = new RecursiveIteratorIterator(
2017-07-02 18:55:27 +02:00
new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST) as $item) {
2018-03-07 15:43:41 +01:00
$currentDirectory = dirname($item->getPathName());
if ($skipDirectory !== $currentDirectory) {
if ($item->isDir()) {
@mkdir($destination.DS.$iterator->getSubPathName());
} else {
copy($item, $destination.DS.$iterator->getSubPathName());
}
}
2017-07-02 18:55:27 +02:00
}
2017-11-16 23:22:55 +01:00
2017-07-02 18:55:27 +02:00
return true;
}
2017-11-16 23:22:55 +01:00
// Delete a file or directory recursive
// The directory is delete
2017-07-02 18:55:27 +02:00
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);
}
2017-11-16 23:22:55 +01:00
// Compress a file or directory
// $source = /home/diego/example
// $destionation = /tmp/example.zip
public static function zip($source, $destination)
{
2017-11-16 23:22:55 +01:00
if (!extension_loaded('zip')) {
return false;
}
if (!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();
}
2017-11-16 23:22:55 +01:00
// Uncompress a zip file
// $source = /home/diego/example.zip
// $destionation = /home/diego/content
public static function unzip($source, $destination)
{
if (!extension_loaded('zip')) {
return false;
}
if (!file_exists($source)) {
return false;
}
$zip = new ZipArchive();
if (!$zip->open($source)) {
return false;
}
$zip->extractTo($destination);
return $zip->close();
}
// Returns the next filename if the filename already exist
public static function nextFilename($path=PATH_UPLOADS, $filename) {
// Clean filename and get extension
$filename = Text::lowercase($filename);
$fileExtension = pathinfo($filename, PATHINFO_EXTENSION);
$filename = pathinfo($filename, PATHINFO_FILENAME);
$filename = Text::replace(' ', '', $filename);
$filename = Text::replace('_', '', $filename);
// Search for the next filename
$tmpName = $filename.'.'.$fileExtension;
if (Sanitize::pathFile($path.$tmpName)) {
$number = 0;
$tmpName = $filename.'_'.$number.'.'.$fileExtension;
while (Sanitize::pathFile($path.$tmpName)) {
$number++;
$tmpName = $filename.'_'.$number.'.'.$fileExtension;
}
}
return $tmpName;
}
2017-12-06 19:14:33 +01:00
}