Sindbad~EG File Manager
<?php
namespace zw\helpers;
class File
{
/**
* Normalizes a file/directory path.
*
* The normalization does the following work:
*
* - Convert all directory separators into `DIRECTORY_SEPARATOR` (e.g. "\a/b\c" becomes "/a/b/c")
* - Remove trailing directory separators (e.g. "/a/b/c/" becomes "/a/b/c")
* - Turn multiple consecutive slashes into a single one (e.g. "/a///b/c" becomes "/a/b/c")
* - Remove ".." and "." based on their meanings (e.g. "/a/./b/../c" becomes "/a/c")
*
* Note: For registered stream wrappers, the consecutive slashes rule
* and ".."/"." translations are skipped.
*
* @param string $path the file/directory path to be normalized
* @param string $ds the directory separator to be used in the normalized result. Defaults to `DIRECTORY_SEPARATOR`.
* @return string the normalized file/directory path
*/
public static function normalizePath($path, $ds = DIRECTORY_SEPARATOR)
{
$path = rtrim(strtr($path, '/\\', $ds . $ds), $ds);
if (strpos($ds . $path, "{$ds}.") === false && strpos($path, "{$ds}{$ds}") === false) {
return $path;
}
// fix #17235 stream wrappers
foreach (stream_get_wrappers() as $protocol) {
if (strpos($path, "{$protocol}://") === 0) {
return $path;
}
}
// the path may contain ".", ".." or double slashes, need to clean them up
if (strpos($path, "{$ds}{$ds}") === 0 && $ds == '\\') {
$parts = array($ds);
} else {
$parts = array();
}
foreach (explode($ds, $path) as $part) {
if ($part === '..' && !empty($parts) && end($parts) !== '..') {
array_pop($parts);
} elseif ($part === '.' || $part === '' && !empty($parts)) {
continue;
} else {
$parts[] = $part;
}
}
$path = implode($ds, $parts);
return $path === '' ? '.' : $path;
}
/**
* Creates a new directory.
*
* This method is similar to the PHP `mkdir()` function except that
* it uses `chmod()` to set the permission of the created directory
* in order to avoid the impact of the `umask` setting.
*
* @param string $path path of the directory to be created.
* @param int $mode the permission to be set for the created directory.
* @param bool $recursive whether to create parent directories if they do not exist.
* @return bool whether the directory is created successfully
* @throws \Exception if the directory could not be created (i.e. php error due to parallel changes)
*/
public static function createDirectory($path, $mode = 0775, $recursive = true)
{
if (is_dir($path)) {
return true;
}
$parentDir = dirname($path);
// recurse if parent dir does not exist and we are not at the root of the file system.
if ($recursive && !is_dir($parentDir) && $parentDir !== $path) {
static::createDirectory($parentDir, $mode, true);
}
try {
if (!mkdir($path, $mode)) {
return false;
}
} catch (\Exception $e) {
if (!is_dir($path)) {// https://github.com/yiisoft/yii2/issues/9288
throw new \Exception("Failed to create directory \"$path\": " . $e->getMessage(), $e->getCode(), $e);
}
}
try {
return chmod($path, $mode);
} catch (\Exception $e) {
throw new \Exception("Failed to change permissions for directory \"$path\": " . $e->getMessage(), $e->getCode(), $e);
}
}
/**
* @param $target
*/
public static function deleteFiles($target)
{
if (is_dir($target)) {
$files = glob($target . '/{,.}*[!.]', GLOB_BRACE | GLOB_MARK); //GLOB_MARK adds a slash to directories returned
foreach ($files as $file) {
self::deleteFiles($file);
}
rmdir($target);
} elseif (is_file($target)) {
unlink($target);
}
}
/**
* @param $source
* @param $destination
* @return bool|string
*/
public static function createZipArchive($source, $destination)
{
$dir = opendir($source);
$result = ($dir === false ? false : true);
if ($result === false) {
return false;
}
$rootPath = realpath($source);
// Initialize archive object
$zip = new \ZipArchive();
$zipfilename = $destination . ".zip";
$x=1;
while(is_file($zipfilename)){
$zipfilename=$destination.'('.$x.').zip';
$x++;
}
$zip->open($zipfilename, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
// Create recursive directory iterator
$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($rootPath), \RecursiveIteratorIterator::LEAVES_ONLY);
foreach ($files as $name => $file) {
// Skip directories (they would be added automatically)
if (!$file->isDir()) {
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
// Add current file to archive
$zip->addFile($filePath, $relativePath);
}
}
// Zip archive will be created only after closing object
$zip->close();
return $zipfilename;
}
}
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists