getPathName()); if ($skipDirectory !== $currentDirectory) { if ($item->isDir()) { @mkdir($destination.DS.$iterator->getSubPathName()); } else { copy($item, $destination.DS.$iterator->getSubPathName()); } } } return true; } // Delete a file or directory recursive // The directory is delete public static function deleteRecursive($source, $deleteDirectory=true) { if (!self::directoryExists($source)) { return false; } foreach (new RecursiveIteratorIterator( new RecursiveDirectoryIterator($source, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST) as $item) { if ($item->isFile()) { unlink($item); } else { rmdir($item); } } if ($deleteDirectory) { return rmdir($source); } return true; } // Compress a file or directory // $source = /home/diego/example // $destionation = /tmp/example.zip public static function zip($source, $destination) { 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(); } // 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 $fileExtension = pathinfo($filename, PATHINFO_EXTENSION); $fileExtension = Text::lowercase($fileExtension); $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 = $number + 1; $tmpName = $filename.'_'.$number.'.'.$fileExtension; } } return $tmpName; } }