Returns the files found under the specified directory and subdirectories.
public static findFiles ( string $dir, array $options = [] ) : array | ||
$dir | string | the directory under which the files will be looked for. |
$options | array | options for file searching. Valid options are: - `filter`: callback, a PHP callback that is called for each directory or file. The signature of the callback should be: `function ($path)`, where `$path` refers the full path to be filtered. The callback can return one of the following values: * `true`: the directory or file will be returned (the `only` and `except` options will be ignored) * `false`: the directory or file will NOT be returned (the `only` and `except` options will be ignored) * `null`: the `only` and `except` options will determine whether the directory or file should be returned - `except`: array, list of patterns excluding from the results matching file or directory paths. Patterns ending with slash ('/') apply to directory paths only, and patterns not ending with '/' apply to file paths only. For example, '/a/b' matches all file paths ending with '/a/b'; and `.svn/` matches directory paths ending with `.svn`. If the pattern does not contain a slash (`/`), it is treated as a shell glob pattern and checked for a match against the pathname relative to `$dir`. Otherwise, the pattern is treated as a shell glob suitable for consumption by `fnmatch(3)` `with the `FNM_PATHNAME` flag: wildcards in the pattern will not match a `/` in the pathname. For example, `views/*.php` matches `views/index.php` but not `views/controller/index.php`. A leading slash matches the beginning of the pathname. For example, `/*.php` matches `index.php` but not `views/start/index.php`. An optional prefix `!` which negates the pattern; any matching file excluded by a previous pattern will become included again. If a negated pattern matches, this will override lower precedence patterns sources. Put a backslash (`\`) in front of the first `!` for patterns that begin with a literal `!`, for example, `\!important!.txt`. Note, the '/' characters in a pattern matches both '/' and '\' in the paths. - `only`: array, list of patterns that the file paths should match if they are to be returned. Directory paths are not checked against them. Same pattern matching rules as in the `except` option are used. If a file path matches a pattern in both `only` and `except`, it will NOT be returned. - `caseSensitive`: boolean, whether patterns specified at `only` or `except` should be case sensitive. Defaults to `true`. - `recursive`: boolean, whether the files under the subdirectories should also be looked for. Defaults to `true`. |
Résultat | array | files found under the directory, in no particular order. Ordering depends on the files system used. |
public static function getAllClasses() { $result = []; foreach (self::getAllAliases() as $alias) { $path = \Yii::getAlias($alias); $files = is_dir($path) ? BaseFileHelper::findFiles($path) : [$path]; foreach ($files as $filePath) { if (!preg_match('/.*\\/[A-Z]\\w+\\.php/', $filePath)) { continue; } $className = str_replace([$path, '.php', '/', '@'], [$alias, '', '\\', ''], $filePath); $result[] = $className; } } return $result; }
public static function getImageAdvert($data, $general = true, $original = false) { $image = []; $base = Url::base(); if ($general) { $image[] = $base . '/uploads/adverts/' . $data['idadvert'] . '/general/small_' . $data['general_image']; } else { $path = \Yii::getAlias("@frontend/web/uploads/adverts/" . $data['idadvert']); $files = BaseFileHelper::findFiles($path); foreach ($files as $file) { if (strstr($file, 'small_') && !strstr($file, 'general')) { $image[] = $base . 'uploads/adverts/' . $data['idadvert'] . '/' . basename($file); } } } return $image; }
public static function getAllClasses() { if (self::$_allClasses !== null) { return self::$_allClasses; } $result = []; foreach (self::getAllAliases() as $alias) { $path = Yii::getAlias($alias); if (!file_exists($path) || is_file($path)) { continue; } $files = BaseFileHelper::findFiles($path, ['except' => ['/yii2-gii/', 'Yii.php']]); foreach ($files as $filePath) { if (!preg_match('/.*\\/[A-Z]\\w+\\.php/', $filePath)) { continue; } $className = str_replace([$path, '.php', '/', '@'], [$alias, '', '\\', ''], $filePath); $result[] = $className; } } return self::$_allClasses = $result; }
/** * После сохранения */ public function afterSave() { $this->path = null; //Сохраняем файл foreach ($this->fields as $field) { $isWeb = $this->_webFile[$field]; $file = null; if ($isWeb || ($file = UploadedFile::getInstance($this->owner, $field))) { //Директория для сохранения $path = $this->getPath($field); $files = BaseFileHelper::findFiles($path); foreach ($files as $cFile) { @unlink($cFile); } $fileName = self::normalize($isWeb ? $this->_names[$field] : $file->name); $filePath = BaseFileHelper::normalizePath($path . '/' . $fileName); // d($filePath); if ($this->_webFile[$field]) { $result = @copy($this->_webFile[$field], $filePath); if (!$result) { $fileName = ''; } } else { $file->saveAs($path . '/' . $fileName); chmod($filePath, 0666); } $this->owner->updateAttributes(array($field => $fileName)); } } $this->setAttach(); }
/** * Cleanup all files in the zip output path. */ protected function cleanup() { $files = BaseFileHelper::findFiles($this->getBaseTempFolderPath(), ['filter' => function ($path) { return time() - filemtime($path) > 30 ? true : false; }, 'recursive' => true]); foreach ($files as $file) { unlink($file); } }
/** * @inheritdoc * @staticvar boolean $searchProcess variable that indicates call method is recursively called or this is the first call. * Fixes logic of parent method. Some file systems may has file for which is_file === false and is_dir === false. * For example: windows file system has it where file has "alien" character set. * * The method returns file paths which have local file system charset. * The `$dir` param must be in local file system charset too. */ public static function findFiles($dir, $options = []) { static $searchProcess = false; if (!$searchProcess) { $searchProcess = true; try { $result = parent::findFiles($dir, $options); } catch (\Exception $ex) { $searchProcess = false; throw $ex; } $searchProcess = false; return $result; } if (!is_dir($dir)) { return []; } return parent::findFiles($dir, $options); }