protected function cleanDir($dir) { $files = new \DirectoryIterator($dir); while ($files->valid()) { if (in_array(substr($files->current(), -2), ['.0', '.1'])) { unlink($dir . '/' . $files->current()); } $files->next(); } }
/** * @ignore */ public static function fromFileScan($uPattern) { $tSep = quotemeta(DIRECTORY_SEPARATOR); $tPos = strrpos($uPattern, $tSep); if ($tSep !== '/' && $tPos === false) { $tSep = '/'; $tPos = strrpos($uPattern, $tSep); } if ($tPos !== false) { $tPattern = substr($uPattern, $tPos + strlen($tSep)); $tPath = substr($uPattern, 0, $tPos + strlen($tSep)); } else { $tPath = $uPattern; $tPattern = ""; } $tTemp = new static(); $tHandle = new \DirectoryIterator($tPath); $tPatExists = strlen($uPattern) > 0; for (; $tHandle->valid(); $tHandle->next()) { if (!$tHandle->isFile()) { continue; } $tFile = $tHandle->current(); if ($tPatExists && !fnmatch($tPattern, $tFile)) { continue; } $tTemp->add(simplexml_load_file($tPath . $tFile)); } return $tTemp; }
/** * Return the current resource. * * @return \com\mohiva\common\io\Resource The current `Resource` object or null if no resource * exists. */ public function current() { if (!$this->valid()) { return null; } $resource = $this->getResourceLoader()->getResourceByType($this->iterator->current(), $this->resourceType); return $resource; }
function rm_recursive($path) { if (!is_dir($path)) { return; } $dir = new DirectoryIterator($path); while ($dir->valid()) { if (!$dir->isDot()) { if ($dir->isDir()) { rm_recursive($path . DIRECTORY_SEPARATOR . $dir->current()); } else { unlink($path . DIRECTORY_SEPARATOR . $dir->current()); } } $dir->next(); } rmdir($path); }
/** * Current. * Please, see \DirectoryIterator::current() method. * * @return mixed */ public function current() { $out = parent::current(); if (null !== $this->_splFileInfoClass && $out instanceof \SplFileInfo) { $out->setInfoClass($this->_splFileInfoClass); $out = $out->getFileInfo(); if ($out instanceof \Hoa\Iterator\SplFileInfo) { $out->setRelativePath($this->getRelativePath()); } } return $out; }
function __construct($link) { $d = new DirectoryIterator($link); while ($d->valid()) { $file = $d->current(); if (!$file->isDot()) { $this->files[] = $file->getFilename(); } $d->next(); } $this->path = $d->getPath(); }
function indexAction() { setcookie("previewTemplateName", false, 0, "/"); $templates = array(); $dir = new DirectoryIterator(Config::get('TEMPLATES_PATH')); while ($dir->valid()) { if (!$dir->isDot()) { $templates[] = (string) $dir->current(); } $dir->next(); } $this->set("alltemplates", $templates); }
function getImages() { $filesInFolder = new DirectoryIterator($this->imagePath); $imageList = array(); $i = 0; while ($filesInFolder->valid()) { $file = $filesInFolder->current(); $filename = $file->getFilename(); $src = "{$this->imagePath}/{$filename}"; $fileInfo = new finfo(FILEINFO_MIME_TYPE); $mimeType = $fileInfo->file($src); if ($mimeType === "image/jpeg") { $imageList[$i++] = $src; } $filesInFolder->next(); } return $imageList; }
function showImages() { $out = "<h1>Image Gallery</h1>"; $out .= "<ul id='images'>"; $folder = "img"; $filesInFolder = new DirectoryIterator($folder); while ($filesInFolder->valid()) { $file = $filesInFolder->current(); $fileName = $file->getFileName(); $src = "{$folder}/{$fileName}"; $fileInfo = new Finfo(FILEINFO_MIME_TYPE); $mimeType = $fileInfo->file($src); if ($mimeType === 'image/jpeg') { $out .= "<li><img src='{$src}' /></li>"; } $filesInFolder->next(); } $out .= "</ul>"; return $out; }
/** * the recursive function that walks through a directory of given path */ protected function WalkDirectory(DirectoryIterator $Directory, $pRecursiveWalk, $depth = 0) { $this->mStringFolderTree = str_repeat(' ', $depth * 5) . $Directory->getPathName() . '<br />'; while ($Directory->valid()) { $node = $Directory->current(); if ($node->isDir() && $node->isReadable() && !$node->isDot()) { if ($this->FolderFilter($Directory, $node)) { $this->ProcessFolder($Directory, $node); } if ($pRecursiveWalk) { $this->mStringFolderTree .= $this->WalkDirectory(new DirectoryIterator($node->getPathname()), $pRecursiveWalk, $depth + 1); } } elseif ($node->isFile()) { if ($this->FileFilter($Directory, $node)) { $this->ProcessFile($Directory, $node); } $this->mStringFolderTree .= str_repeat(' ', (1 + $depth) * 5) . $node->getFilename(); } $Directory->next(); } return $this->mStringFolderTree; }
function showImages() { $out = "<h1>Image Gallery</h1>"; $out .= "<ul id='images'"; $folder = "img"; $filesInFolder = new DirectoryIterator($folder); //Iterate through the directory for images //with different file types while ($filesInFolder->valid()) { $file = $filesInFolder->current(); $filename = $file->getFilename(); $src = "{$folder}/{$filename}"; $fileInfo = new Finfo(FILEINFO_MIME_TYPE); $mimeType = $fileInfo->file($src); if ($mimeType === 'image/jpeg' || $mimeType === 'image/pjpeg' || $mimeType === 'image/png' || $mimeType === 'image/gif') { $out .= "<li><img src='{$src}' /></li>"; } $filesInFolder->next(); } $out .= "</ul>"; return $out; }
function WalkDirectory(DirectoryIterator $iter, $depth = 0) { $return = str_repeat(' ', $depth * 5) . $iter->getPathName() . "\n"; while ($iter->valid()) { $node = $iter->current(); if ($node->isDir() && $node->isReadable() && !$node->isDot()) { $return .= WalkDirectory(new DirectoryIterator($node->getPathname()), $depth + 1); } elseif ($node->isFile()) { $return .= str_repeat(' ', (1 + $depth) * 5) . $node->getFilename() . "\n"; } // Rename wf_ to admin_ $oldName = $node->getFilename(); if (strpos($oldName, 'wf_') === 0) { $newFileName = str_replace('wf_', 'admin_', $node->getFilename()); $path = str_replace('/www/wildflower/', '', $node->getPathname()); $cmd = 'git mv ' . $path . ' ' . str_replace($oldName, $newFileName, $path) . "\n"; $return .= $cmd; exec($cmd); } $iter->next(); } return $return; }
/** * Copy a directory within all its items. * * @param string $source The source directory * @param string $target The target to create * @param array $exclude List of files / folders that should not be copyed * * @return void */ public function copyDirectory($source, $target, $exclude = array()) { // first check for target itself self::createDirectory($target); $iterator = new DirectoryIterator($source); while ($iterator->valid()) { $item = $iterator->current(); // create new file if ($iterator->isFile()) { self::copyFile($source . '/' . $item, $target); } // create folder recursive if (!$iterator->isDot() && $iterator->isDir() && !in_array($item, $exclude)) { self::copyDirectory($source . '/' . $item, $target . '/' . $item); } $iterator->next(); } }
<?php $dir = new DirectoryIterator(__DIR__ . '/../../sample_dir'); $files = array(); // order changes per machine while ($dir->valid()) { if (!$dir->isDot()) { $files[] = $dir->current() . "\n"; } $dir->next(); } asort($files); var_dump(array_values($files));
/** * Cleanup the test directory output folder * * @param string $dir The directory to clean up * * @return void */ protected function _cleanUp($dir) { $iterator = new DirectoryIterator($dir); while ($iterator->valid()) { // delete file if ($iterator->isFile()) { unlink($dir . '/' . $iterator->current()); } // delete folder recursive if (!$iterator->isDot() && $iterator->isDir()) { $this->_cleanUp($dir . '/' . $iterator->current()); rmdir($dir . '/' . $iterator->current()); } $iterator->next(); } unset($iterator); }
/** * Получить очередную пару файлов для слияния. * * @param \DirectoryIterator $files * @param $suffix * @return array */ protected function nextPair(\DirectoryIterator $files, $suffix) { $res = []; while ($files->valid() && count($res) < 2) { $fileSuffix = substr($files->current(), -strlen($suffix)); if (strcmp($fileSuffix, $suffix) == 0) { $res[] = $files->getPathname(); } $files->next(); } return array_pad($res, 2, null); }
| eMail: support at myndworx dot com | | Website: http://www.nexoscms.org and http://www.myndworx.com | +-------------------------------------------------------------------+ |Original Copyright below | +-------------------------------------------------------------------+ */ /********************************************* Dragonfly CMS, Copyright (c) 2004 by CPGNuke Dev Team http://dragonflycms.org Released under GNU GPL version 2 or any later version $Source: /cvs/html/includes/load/cli.php,v $ $Revision: 1.1 $ $Author: nanocaiordo $ $Date: 2011/04/17 06:50:09 $ **********************************************/ if (!defined('CPG_NUKE')) { exit; } $_get = array(); $argv = array(0 => 'index.php', 1 => 'file', 2 => 'param', 2 => 'key', 3 => 'val'); array_shift($array); $dir = new DirectoryIterator(CORE_PATH . 'load/cli'); while ($dir->valid()) { if (!$dir->isDot() && ($file = $dir->current() && $array[1] === $file->getFilename())) { $file = CORE_PATH . 'load/cli/' . $array[1]; exec(`php -a `); $array = array_chunk($array, 2); } $dir->next(); }
echo '<tr><td>isDir()</td><td> '; var_dump($item->isDir()); echo '</td></tr>'; echo '<tr><td>isLink()</td><td> '; var_dump($item->isLink()); echo '</td></tr>'; echo '<tr><td>getFileInfo()</td><td> '; var_dump($item->getFileInfo()); echo '</td></tr>'; echo '<tr><td>getPathInfo()</td><td> '; var_dump($item->getPathInfo()); echo '</td></tr>'; echo '<tr><td>openFile()</td><td> '; var_dump($item->openFile()); echo '</td></tr>'; echo '<tr><td>setFileClass()</td><td> '; var_dump($item->setFileClass()); echo '</td></tr>'; echo '<tr><td>setInfoClass()</td><td> '; var_dump($item->setInfoClass()); echo '</td></tr>';*/ } } echo '</table>'; // while 循环 $directoryIt->rewind(); while ($directoryIt->valid()) { // 过滤 . 和 .. if (!$directoryIt->isDot()) { echo $directoryIt->key(), '=>', $directoryIt->current(), '<br />'; } $directoryIt->next(); } // 获得该目录的所有文件和下级文件夹的文件 /*$rdIt = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('/data/www/yii2/learn/backend')); foreach ($rdIt AS $name => $object) { echo $object.'<br/>'; }*/ echo '----------------------------------- DirectoryIterator END ---------------------------------', '<br />'; echo '--------------------------------- SimpleXMLIterator START-----------------------------------', '<br />'; /** * SimpleXMLIterator 遍历xml文件 * */ try {
/** * Return an instance of SplFileInfo with support for relative paths * * @return SplFileInfo File information */ public function current() { $relativePath = str_replace($this->root . '/', '', parent::current()->getPath()); $relativePathname = str_replace($this->root . '/', '', parent::current()->getPathname()); return new SplFileInfo(parent::current()->getPathname(), $relativePath, $relativePathname); }
public function loadValidatorsByDir($dir) { $dir = new DirectoryIterator($dir); while ($dir->valid()) { if (!$dir->isDot() && $dir->isDir()) { $validatorfile = $dir->getPath() . '/' . $dir->current() . '/Validator.php'; $validatorclass = 'Config_Validator_' . $dir->current() . 'Node_PartyCal'; $this->loadValidator($validatorclass, $validatorfile); } $dir->next(); } }
function current() { echo __METHOD__ . "\n"; return parent::current(); }
<?php mkdir(__DIR__ . DIRECTORY_SEPARATOR . 'tmp'); touch(__DIR__ . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . 'a'); touch(__DIR__ . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . 'b'); $d = new DirectoryIterator(__DIR__ . DIRECTORY_SEPARATOR . 'tmp'); $d->seek(0); $path0 = $d->current()->getPathname(); $d->seek(1); $path1 = $d->current()->getPathname(); $d->seek(2); $path2 = $d->current()->getPathname(); $d->seek(0); var_dump($path0 === $d->current()->getPathname()); $d->seek(1); var_dump($path1 === $d->current()->getPathname()); $d->seek(2); var_dump($path2 === $d->current()->getPathname()); $d->seek(0); var_dump($path0 === $d->current()->getPathname());
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <?php $dir = new DirectoryIterator("E:/AppServ"); echo "<table border='1' width='500'>"; echo "<tr><th>目录/文件名</th><th>大小</th><th>操作</th></tr>"; while ($dir->valid()) { echo "<tr>"; $d = $dir->current(); if ($dir->isDir()) { echo "<td><a href=''>{$d}</a></td>"; echo "<td> </td>"; echo "<td> </td>"; } else { echo "<td>" . $d . "</td>"; echo "<td>" . $dir->getSize() . "byte</td>"; echo "<td><a href=''>删除</a></td>"; } echo "</tr>"; $dir->next(); } echo "</table>"; ?> </body> </html>
private function goPastDotsIfNeeded() { if ($this->flags & FilesystemIterator::SKIP_DOTS) { $f = parent::current(); while ($f && $f->isDot()) { parent::next(); $f = parent::current(); } } }
public function testClear() { $cacheDir = AgaviConfig::get('core.cache_dir') . DIRECTORY_SEPARATOR . 'config'; AgaviConfigCache::clear(); $directory = new DirectoryIterator($cacheDir); foreach ($directory as $item) { if ($directory->current()->isDot()) { continue; } $this->fail(sprintf('Failed asserting that the cache dir "%1$s" is empty, it contains at least "%2$s"', $cacheDir, $item->getFileName())); } }
<?php if (isset($uploadMessage) === false) { $uploadMessage = "Upload a new image"; } $imagesAsHTML = "<h1>Images</h1>"; $imagesAsHTML .= "<dl id='images'>"; $folder = "img"; $filesInFolder = new DirectoryIterator($folder); while ($filesInFolder->valid()) { $file = $filesInFolder->current(); $filename = $file->getFilename(); $src = "{$folder}/{$filename}"; $fileInfo = new Finfo(FILEINFO_MIME_TYPE); $mimeType = $fileInfo->file($src); if ($mimeType === 'image/jpeg') { $href = "admin.php?page=images&delete-image={$src}"; $imagesAsHTML .= "<dt><img src='{$src}' /></dt>\n <dd>Source: {$src} <a href='{$href}'>delete</dd>"; } $filesInFolder->next(); } $imagesAsHTML .= "</dl>"; return "\n <form method='post' action='admin.php?page=images' enctype='multipart/form-data'>\n <p>{$uploadMessage}</p>\n <input type='file' name='image-data' accept='image/jpeg' />\n <input type='submit' name='new-image' value='upload' />\n </form>\n <div>\n {$imagesAsHTML}\n </div>\n";
/** * @param int $offset * @param int $limit * @param int|string|float $search * @return array */ protected function paginateFilesSearch($offset, $limit, $search) { $filei = 0; $listTotal = 0; $list = array(); if ($offset === 0) { $offset = -1; } else { $filei = 1; } $search = (string) $search; while (parent::valid() && $listTotal < $limit) { $current = parent::current(); if ($current->isFile() && stripos($current->getFilename(), $search) !== false && $filei++ > $offset) { $list[] = clone $current; $listTotal++; } parent::next(); } parent::rewind(); return $list; }
<?php /*** create a new iterator object ***/ $it = new DirectoryIterator('./'); /*** loop directly over the object ***/ while ($it->valid()) { /*** check if value is a directory ***/ if ($it->isDir()) { /*** echo the key and current value ***/ echo $it->key() . ' -- ' . $it->current() . '<br />'; } /*** move to the next iteration ***/ $it->next(); }