function zipData($source, $destination) { if (extension_loaded('zip')) { if (file_exists($source)) { $zip = new ZipArchive(); if ($zip->open($destination, ZIPARCHIVE::CREATE)) { $source = realpath($source); if (is_dir($source)) { $iterator = new RecursiveDirectoryIterator($source); // skip dot files while iterating $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 . '/')); } else { if (is_file($file)) { $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file)); } } } } else { if (is_file($source)) { $zip->addFromString(basename($source), file_get_contents($source)); } } } echo $destination . ' zip: successfully...'; echo "\n"; return $zip->close(); } } return false; }
/** * get callable module list * * @return array */ public function getCallableModules() { $dirIterator = new \RecursiveDirectoryIterator($this->path, \FilesystemIterator::NEW_CURRENT_AND_KEY | \FilesystemIterator::SKIP_DOTS); $modules = array(); foreach ($dirIterator as $k => $v) { $doVotedModule = false; if ($dirIterator->hasChildren()) { foreach ($dirIterator->getChildren() as $key => $value) { $entension = $value->getExtension(); if (!$entension || 'php' !== $entension) { continue; } $fileBasename = $value->getBasename('.php'); $module_to_be = $v->getBasename(); $expectedClassName = $this->baseNamespace . NAMESPACE_SEPARATOR . $module_to_be . NAMESPACE_SEPARATOR . $fileBasename; Loader::getInstance()->import($value->__toString()); if (!class_exists($expectedClassName, false)) { // not a standard class file! continue; } if (!$doVotedModule) { $modules[] = $module_to_be; $doVotedModule = true; } } } } return $modules; }
public function index() { parent::index(); $album = empty($this->parameters) ? "" : array_shift($this->parameters); $dir = new \RecursiveDirectoryIterator(dirname(__DIR__) . $this->basedir . $album); if (empty($album)) { $albums = array(); foreach ($dir as $name => $object) { $shortname = basename($name); if ($shortname != ".." && $shortname != ".") { $temp_album = new \DirectoryIterator($name); foreach ($temp_album as $img) { if ($img->isFile()) { break; } } $albums[$dir->getCTime()] = array("name" => $shortname, "image" => $this->basedir . $shortname . "/" . $img, "link" => "/fotos/index/" . $shortname); } } ksort($albums); $this->context["albums"] = $albums; } else { $pictures = array(); foreach ($dir as $image) { $shortname = basename($image); if ($shortname != ".." && $shortname != ".") { $pictures[] = $this->basedir . $album . "/" . $shortname; } } $this->context["pictures"] = $pictures; $this->context["album"] = $album; } }
public function listPosts() { $path = null; $timestamp = null; $dirname = $this->settings->posts_dir; $dir = new \RecursiveDirectoryIterator($dirname); $dir->setFlags(\RecursiveDirectoryIterator::SKIP_DOTS); ob_start(); ?> <div class="row"> <table class="post-table"> <tr> <th>Name</th> <th>Date created</th> </tr> <?php foreach ($dir as $fileinfo) { if ($fileinfo->getMTime() > $timestamp) { // current file has been modified more recently // than any other file we've checked until now $path = $fileinfo->getFilename(); $modified = $fileinfo->getMTime(); $created = $fileinfo->getCTime(); $linkUrl = preg_replace('/\\.[^.\\s]{1,4}$/', '', $path); echo "<tr><td><a href='?action=edit&post=" . $linkUrl . "'>" . $path . "</a></td>"; echo "<td>" . date("F d Y H:i:s.", $created) . "</td></tr>"; } } ?> </table> </div> <style> .post-table { width: 80%; margin: 50px auto; text-align: center; border: solid 1px #ddd; } .post-table th { font-weight: bold; } .post-table th, .post-table td { text-align: left; padding: 5px; border: solid 1px #ddd; } </style> <?php return ob_get_clean(); }
function show_maps() { $maps = array(); $it = new RecursiveDirectoryIterator('../maps'); foreach (new RecursiveIteratorIterator($it) as $file) { if (!$it->isDot()) { array_push($maps, basename($file)); } } echo json_encode($maps); }
/** * Get all sub directory * @param string $path The base path want to find * @return array Array of path of sub directory founded */ public static function getAllSubDirectory($path) { $rdi = new RDIterator($path, RDIterator::SKIP_DOTS); $subDirs = []; foreach ($rdi as $item) { if ($item->isDir()) { $subDirs[] = $rdi->getPathname(); } } return $subDirs; }
/** * Create Pages object instance from path * * @param string $path * @param array|callable $filter * @param string $class * @return \cms\Pages */ public static function from($path, $filter = ['index', '404'], $class = Page::class) { $iterator = new \RecursiveDirectoryIterator(realpath($path), \RecursiveDirectoryIterator::SKIP_DOTS); if (class_exists($class)) { $iterator->setInfoClass($class); } $filter = is_callable($filter) ? $filter : function (Page $item) use($filter) { return $item->isValid((array) $filter); }; return new self(new \RecursiveCallbackFilterIterator($iterator, $filter)); }
function routesInDirectory($app = '') { $routeDir = app_path('Http/Routes/' . $app . ($app !== '' ? '/' : NULL)); $iterator = new RecursiveDirectoryIterator($routeDir); $iterator->setFlags(RecursiveDirectoryIterator::SKIP_DOTS); foreach ($iterator as $route) { $isDotFile = strpos($route->getFilename(), '.') === 0; if (!$isDotFile && !$route->isDir()) { require $routeDir . $route->getFilename(); } } }
/** * A function to (optionally) recursive through a root directory, * retrieving image paths * * @param $folder * The root folder to start the scanning * @param $enable_chidren * Boolean value to enable scanning through child folders, default to false * * @return * An array of image paths * * @todo * Use more robust php recursive directory search options */ static function getImagePaths($folder, $enable_chidren = FALSE) { $image_array = array(); $iterator = new RecursiveDirectoryIterator($folder); $image_array = array_merge($image_array, VSMGFileHelper::getDirectoryImages($folder)); if ($enable_chidren) { foreach ($iterator as $file) { if (is_dir($file) && !$iterator->isDot()) { // echo "VSMGFileHelper::getImagePaths DIR: $file\n"; $image_array = array_merge($image_array, VSMGFileHelper::getDirectoryImages($file)); } } } return $image_array; }
/** * deletes a folder recursively * * @param string $path the folder to delete * * @since 1.0 */ protected static function _deleteFolder($path) { /** * @var \SplFileInfo $item */ $dir = new \RecursiveDirectoryIterator($path); while ($dir->valid()) { $item = $dir->current(); if (!in_array($item->getFilename(), array('.', '..'))) { $isFile = $item->isFile(); $isFile ? unlink($item->getRealPath()) : self::_deleteFolder($item->getRealPath()); } $dir->next(); } rmdir($path); }
public function next() { parent::next(); while ($this->isDot()) { parent::next(); } }
/** Override of getChildren in \RecursiveDirectoryIterator, to skip directories with insufficient rights to access * @internal */ function getChildren() { try { return parent::getChildren(); } catch (\UnexpectedValueException $e) { return new \RecursiveArrayIterator(array()); } }
/** * {@inheritdoc} */ public function current() { $current = parent::current(); if ($this->normalizeCurrent) { $current = str_replace('\\', '/', $current); } return $current; }
/** * import * * @param string $tsvDirPath * @access public * @return void */ public function import($tsvDirPath = null) { if (is_null($tsvDirPath)) { $tsvDirPath = realpath(__DIR__ . '/../../../../../../masterData'); } $iterator = new \RecursiveDirectoryIterator($tsvDirPath); foreach ($iterator as $file) { if (!$iterator->hasChildren()) { continue; } $databaseName = $file->getFileName(); $con = $this->getConnection($databaseName); $con->exec('set foreign_key_checks = 0'); $this->importFromTsvInDir($iterator->getChildren(), $con); $con->exec('set foreign_key_checks = 1'); } }
/** * Do nothing for non rewindable stream */ public function rewind() { if (false === $this->isRewindable()) { return; } // @see https://bugs.php.net/bug.php?id=49104 parent::next(); parent::rewind(); }
/** * Retrieves old CSS files and list them * * @param $ttl int * @return array */ protected function getOutdatedFiles($ttl) { $outdated = array(); $time = time(); $dir = new RecursiveDirectoryIterator($this->configuration->getUploadDir()); $dir->setFlags(RecursiveDirectoryIterator::SKIP_DOTS); /* * Collecting CSS files */ $files = new RegexIterator(new RecursiveIteratorIterator($dir), '#.css#U', RecursiveRegexIterator::ALL_MATCHES); /* * Checking expiry */ foreach ($files as $filepath => $match) { filemtime($filepath) + $ttl < $time ? array_push($outdated, $filepath) : null; } return $outdated; }
public function __construct(array $sources) { foreach ($sources as $source) { if ($source instanceof \SplFileInfo) { $this->children[] = $source; } elseif (is_string($source) && ($source = realpath($source))) { if (is_file($source)) { $child = new SourceFileInfo($source); $child->setInfoClass(SourceFileInfo::class); $this->children[] = $child; } elseif (is_dir($source)) { $child = new \RecursiveDirectoryIterator($source); $child->setInfoClass(SourceFileInfo::class); $this->children[] = $child; } } } }
public function zipData($source, $destination) { global $Language; $archiveName = 'backup-' . time() . '.zip'; // Archive name if (extension_loaded('zip')) { if (file_exists($source)) { $zip = new ZipArchive(); if ($zip->open($destination, ZIPARCHIVE::CREATE)) { $source = realpath($source); if (is_dir($source)) { $iterator = new RecursiveDirectoryIterator($source); // skip dot files while iterating $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 . '/')); } else { if (is_file($file)) { $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file)); } } } } else { if (is_file($source)) { $zip->addFromString(basename($source), file_get_contents($source)); } } } if (!$zip->close()) { Alert::set($Language->get("There was a problem writing the ZIP archive.")); Redirect::page('admin', 'configure-plugin/pluginBackup'); } else { Alert::set($Language->get("Successfully created the ZIP Archive!")); Redirect::page('admin', 'configure-plugin/pluginBackup'); } // close the zip file $zip->close(); } } return false; }
public function getChildren() { $children = parent::getChildren(); if (is_object($children)) { echo get_class($children) . " {$children}\n"; } else { echo gettype($children) . " {$children}\n"; } return $children; }
/** * @param string $dir * @return FileIndex */ public static function make($dir) { $dirIterator = new \RecursiveDirectoryIterator($dir); $dirIterator->setFlags(\RecursiveDirectoryIterator::SKIP_DOTS); $recursiveIterator = new \RecursiveIteratorIterator($dirIterator, \RecursiveIteratorIterator::SELF_FIRST); $results = array(); foreach ($recursiveIterator as $file) { /** @var \SplFileInfo $file */ // ignore non files if (!$file->isFile()) { continue; } // ignore non php files $ext = strtolower($file->getExtension()); if ($ext !== 'php') { continue; } $results[] = $file->getPathname(); } return new self($results); }
public function __construct($path, $flags = null, $recursive = false) { if (is_bool($flags)) { $recursive = $flags; $flags = null; } if ($flags === null) { $flags = \FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_FILEINFO; } $this->recursive = $recursive; parent::__construct($path, $flags); }
function folder_files($path) { $iterator = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS); $files = []; foreach ($iterator as $item) { if ($item->isDir()) { continue; } $files[] = $iterator->getPathName(); } return $files; }
public function cmd_artifact(array $params = array()) { \System\Directory::check(BASE_DIR . static::DIR_PACKAGES); $target = BASE_DIR . static::DIR_PACKAGES . '/artifact.tar'; $result = $target . '.gz'; if (isset($params[0])) { $target = $params[0]; } if (file_exists($target)) { unlink($target); } if (file_exists($result)) { unlink($result); } $iter = new \RecursiveDirectoryIterator(BASE_DIR); $iter->setFlags(\FileSystemIterator::SKIP_DOTS); $iter = new ProjectDirectoryRecursiveIterator($iter); $iter = new \RecursiveIteratorIterator($iter); $archive = new \PharData($target); $archive->buildFromIterator($iter, BASE_DIR); $archive->compress(\Phar::GZ); unlink($target); }
public function current() { $file = parent::current(); if ($file instanceof SplFileInfo) { // Wrap into sfFilebasePluginFile to provide additional methods // for analyzing file-type $file = new sfFilebasePluginFile($file, $this->filebase); if ($file->isDir()) { $file = new sfFilebasePluginDirectory($file, $this->filebase); } elseif ($file->isImage()) { $file = new sfFilebasePluginImage($file, $this->filebase); } } return $file; }
/** * Data provider for testExamples method. * * Assumes that an `examples` directory exists inside parent directory. * This examples directory should contain any number of subdirectories, each of which contains * three files: one Mustache class (.php), one Mustache template (.mustache), and one output file * (.txt). * * This whole mess will be refined later to be more intuitive and less prescriptive, but it'll * do for now. Especially since it means we can have unit tests :) * * @access public * @return array */ public function getExamples() { $basedir = dirname(__FILE__) . '/../examples/'; $ret = array(); $files = new RecursiveDirectoryIterator($basedir); while ($files->valid()) { if ($files->hasChildren() && ($children = $files->getChildren())) { $example = $files->getSubPathname(); $class = null; $template = null; $output = null; foreach ($children as $file) { if (!$file->isFile()) { continue; } $filename = $file->getPathInfo(); $info = pathinfo($filename); switch ($info['extension']) { case 'php': $class = $info['filename']; include_once $filename; break; case 'mustache': $template = file_get_contents($filename); break; case 'txt': $output = file_get_contents($filename); break; } } $ret[$example] = array($class, $template, $output); } $files->next(); } return $ret; }
/** * Not using recursiveDirectoryIterator to savely ignore unreadable directories * * @param RecursiveDirectoryIterator $iter */ protected function iterateFiles(RecursiveDirectoryIterator $iter) { foreach ($iter as $file) { if (!is_readable($file)) { continue; } else { if (!$iter->isDot()) { if (is_file((string) $file)) { if (!$this->isIgnored($file) && !$this->isIgnoredDirectory($file)) { $this->results[] = new Robo47_DirectoryHasher_Result_File($file); } } else { if (is_dir($file)) { $this->iterateFiles(new RecursiveDirectoryIterator((string) $file)); } } } } } }
private function load_tests(RecursiveDirectoryIterator $dir) { while ($dir->valid()) { $current = $dir->current(); if ($dir->isFile() && preg_match("/(.)Test\\.php\$/", $current->getFilename(), $matches)) { // XXX: handle errors include $current->getPathname(); $x = explode('.', $current->getFilename()); $class = $x[0]; $rclass = new ReflectionClass($class); if ($rclass->getParentClass()->getName() == 'UnitTest') { $this->cases[] = $rclass; } } elseif ($dir->hasChildren() && preg_match("/^\\./", $current->getFilename(), $matches) == 0) { $this->load_tests($dir->getChildren()); } $dir->next(); } }
/** * fills the list of availible files, with DirectoryIterator object as value * * @return void */ protected function scanFilesObj() { if (!empty($this->filesObj)) { return; } if ($this->recursive) { $it = new RecursiveIteratorIterator($this->obj, RecursiveIteratorIterator::CHILD_FIRST); foreach ($it as $filename => $obj) { if ($it->isDot()) { continue; } $this->filesObj[$filename] = $obj; } } else { foreach ($this->obj as $filename => $obj) { if ($this->obj->isDot()) { continue; } $this->filesObj[$obj->getFilename()] = $obj; } } }
/** * parse this directory * * @return none */ protected function parseDir() { $this->clear(); $iter = new RecursiveDirectoryIterator($this->getPath()); while ($iter->valid()) { $curr = (string) $iter->getSubPathname(); if (!$iter->isDot() && $curr[0] != '.') { $this->addItem(Varien_Directory_Factory::getFactory($iter->current(), $this->getRecursion(), $this->getRecursionLevel())); } $iter->next(); } }
/** * Deletes all files, directories and subdirectories from the specified * directory. The passed directory itself won't be deleted though. * * @param string $path Path to the directory which shall be emptied. * @return void * @throws Exception * @see removeDirectoryRecursively() */ public static function emptyDirectoryRecursively($path) { if (!is_dir($path)) { throw new \TYPO3\Flow\Utility\Exception('"' . $path . '" is no directory.', 1169047616); } if (self::is_link($path)) { if (self::unlink($path) !== TRUE) { throw new \TYPO3\Flow\Utility\Exception('Could not unlink symbolic link "' . $path . '".', 1323697654); } } else { $directoryIterator = new \RecursiveDirectoryIterator($path); foreach ($directoryIterator as $fileInfo) { if (!$fileInfo->isDir()) { if (self::unlink($fileInfo->getPathname()) !== TRUE) { throw new \TYPO3\Flow\Utility\Exception('Could not unlink file "' . $fileInfo->getPathname() . '".', 1169047619); } } elseif (!$directoryIterator->isDot()) { self::removeDirectoryRecursively($fileInfo->getPathname()); } } } }