protected function scanFolder($folder, &$position, $forFolders = true, $threshold_key = 'dir', $threshold_default = 50) { $registry = AEFactory::getConfiguration(); // Initialize variables $arr = array(); $false = false; if (!is_dir($folder) && !is_dir($folder . '/')) { return $false; } try { $di = new DirectoryIterator($folder); } catch (Exception $e) { $this->setWarning('Unreadable directory ' . $folder); return $false; } if (!$di->valid()) { $this->setWarning('Unreadable directory ' . $folder); return $false; } if (!empty($position)) { $di->seek($position); if ($di->key() != $position) { $position = null; return $arr; } } $counter = 0; $maxCounter = $registry->get("engine.scan.large.{$threshold_key}_threshold", $threshold_default); while ($di->valid()) { if ($di->isDot()) { $di->next(); continue; } if ($di->isDir() != $forFolders) { $di->next(); continue; } $ds = $folder == '' || $folder == '/' || @substr($folder, -1) == '/' || @substr($folder, -1) == DIRECTORY_SEPARATOR ? '' : DIRECTORY_SEPARATOR; $dir = $folder . $ds . $di->getFilename(); $data = _AKEEBA_IS_WINDOWS ? AEUtilFilesystem::TranslateWinPath($dir) : $dir; if ($data) { $counter++; $arr[] = $data; } if ($counter == $maxCounter) { break; } else { $di->next(); } } // Determine the new value for the position $di->next(); if ($di->valid()) { $position = $di->key() - 1; } else { $position = null; } return $arr; }
private function _filestackProcessDir($dir) { $Iterator = new \DirectoryIterator($dir); if ($this->_filestackKey > 0) { $Iterator->seek($this->_filestackKey); } while ($Iterator->valid()) { // timeout check: if ($this->intervalLimitIsReached() === true) { $this->_Dirstack->save(); $this->_Filestack->save(); $this->setMessage('Filestack creation: ' . $this->_filesInStack . ' files processed.'); return false; } $this->_filestackKey = $Iterator->key(); $this->Storage->set('filestackKey', $this->_filestackKey, 'filecheck'); // skip dots: if ($Iterator->isDot()) { $Iterator->next(); continue; } // if is directory save to database for later processing: $currentPath = $Iterator->getPathname(); if ($Iterator->isDir()) { $this->_Dirstack->addPath($currentPath); $Iterator->next(); continue; } // filesize check: $filesize = $Iterator->getSize(); if (empty($filesize) || $filesize > $this->sizeFilter) { $Iterator->next(); continue; } // filetype check: if ($this->_extensionCheck === true) { $fileExtension = $Iterator->getExtension(); if (empty($fileExtension) || !isset($this->_allowedExtensions[$fileExtension])) { $Iterator->next(); continue; } } // add file to stack: $this->_Filestack->addPath($currentPath); // create hash (if not yet done): if ($this->_createFilehashDb === true) { $pathhash = sha1($currentPath); $filehash = sha1_file($currentPath); $this->Database->setQuery("INSERT IGNORE INTO #__wm_filehashes (pathhash, filehash, mode) VALUES(" . $this->Database->q($pathhash) . "," . $this->Database->q($filehash) . ", " . $this->Database->q($this->runMode) . ")")->execute(); } $this->_filesInStack++; $this->Storage->set('filesInStack', $this->_filesInStack, 'filecheck'); $Iterator->next(); } unset($Iterator); // current dir completed -> delete from stack: $this->_Dirstack->save(); $this->_Dirstack->clear(); $this->_Filestack->save(); $this->_Filestack->clear(); $this->Database->setQuery("DELETE FROM #__wm_dirstack WHERE path = " . $this->Database->q($dir) . "AND mode = " . $this->Database->q($this->runMode))->execute(); $this->_filestackKey = 0; // select next dir from stack: $this->Database->setQuery("SELECT path FROM #__wm_dirstack WHERE mode = " . $this->Database->q($this->runMode) . " LIMIT 1")->execute(); $row = $this->Database->getRow(); if (empty($row)) { return true; } $this->_filestackPath = $row->path; $this->Storage->set('filestackPath', $this->_filestackPath, 'filecheck'); return $this->_filestackProcessDir($row->path); }
/** * Get list of files for file browser * @param int $seek * @param int $limit * @param string $filter * @param bool $secure use secure folder instead of repository root * @return array */ public function getAvailableFiles($seek, $limit, $filter, $filterExtensions, $secure = false, $subdir = null) { $answer = array(); if ($subdir && substr($subdir, -1) != '/') { $subdir .= '/'; } $repositoryDir = $this->getPath($secure, $subdir); $iterator = new \DirectoryIterator($repositoryDir); $iterator->seek($seek); while ($iterator->valid() && count($answer) < $limit) { if ($iterator->isFile()) { $fileData = $this->getFileData($iterator->getFilename(), $secure, $subdir); $append = null; switch ($filter) { case 'image': if (in_array($fileData['ext'], $this->supportedImageExtensions)) { $append = $fileData; } break; default: $append = $fileData; break; } if ($filterExtensions && !in_array($fileData['ext'], $filterExtensions)) { $append = null; } if ($append) { $answer[] = $append; } } $iterator->next(); } return $answer; }
<?php $di = new DirectoryIterator(__DIR__); $di->seek(2); $n = 0; while ($di->valid()) { $n++; $di->next(); } echo "With seek(2) we get {$n}\n"; $di->seek(0); $m = 0; while ($di->valid()) { $m++; $di->next(); } echo "With seek(0) we get {$m}\n"; $o = 0; $di->rewind(); while ($di->valid()) { $o++; $di->next(); } echo "Without seek we get {$o}\n"; $p = 0; $di->seek($o + 1); while ($di->valid()) { $p++; $di->next(); } var_dump($n !== $m, $m === $o, $p === 0);
<?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());
<?php $dir = new DirectoryIterator('./images'); session_start(); if (!isset($_SESSION['imageid'])) { $_SESSION['imageid'] = 0; } $dir->seek($_SESSION['imageid']); do { $dir->next(); //loop back to 0 if (!$dir->valid()) { $dir->seek(0); } } while (!$dir->isFile() || !$dir->valid() || !exif_imagetype($dir->getPathname())); //it's not an image $_SESSION['imageid'] = $dir->key(); //echo $dir->key(); echo './images/' . $dir->getFilename(); //imagejpeg('./images/'.$fileinfo-getFilename(), NULL,100);
/** * Overridden to not crash PHP 5.2, because DirectoryIterator::seek was added in 5.3. * * @link http://lxr.php.net/xref/PHP_5_3/ext/spl/spl_directory.c#807 */ public function seek($position) { if (is_callable(array('parent', 'seek'))) { parent::seek($position); return; } if ($this->key() > $position) { $this->rewind(); } while ($this->key() < $position) { if (!$this->valid()) { return; } $this->next(); } }
<?php $dit = new DirectoryIterator(__DIR__); $dit->seek(5); echo $dit->key(), PHP_EOL; $dit->seek(4); echo $dit->key(), PHP_EOL;