Skip to content

Instantly share code, notes, and snippets.

@juner
Last active June 26, 2019 01:38
Show Gist options
  • Save juner/94c34f09542f9ffc722c27eb09610821 to your computer and use it in GitHub Desktop.
Save juner/94c34f09542f9ffc722c27eb09610821 to your computer and use it in GitHub Desktop.
<?php
// Your code here!
touch('test.text');
mkdir('test/hoge/fuga');
mkdir('test');
touch('test/test2.text');
mkdir('test/test');
touch('test/test/test3.hoge');
mkdir('test/test2');
touch('test/test2/test2-2.text');
$fnc = null;
$fnc = function (string $dir, string $file_pattern = "{*.*}", ?int $count = null) use (&$fnc) {
$count = $count ?? -1;
$_files = [];
$files_pattern = $dir.$file_pattern;
$files = glob($files_pattern, GLOB_NOSORT | GLOB_BRACE);
if (is_array($files))
foreach ($files as $file_name)
$_files[] = $file_name;
if ($count == 0)
return $_files;
$dirs_pattern = $dir.'*/';
$dirs = glob($dirs_pattern, GLOB_ONLYDIR | GLOB_MARK);
$tmp;
if ($count > 0)
$count--;
if (is_array($dirs))
foreach($dirs as $dir_name)
if ($tmp = $fnc($dir_name, $file_pattern, $count))
$_files = array_merge($_files,$tmp);
return $_files;
};
echo "search *.text \r\n";
foreach($fnc("./", "{*.text}") as $file_name) {
echo $file_name . "\r\n";
}
echo "\r\n";
echo "search count: 1 *.text \r\n";
foreach($fnc("./", "{*.text}", 1) as $file_name) {
echo $file_name . "\r\n";
}
echo "\r\n";
echo "search *.hoge \r\n";
foreach($fnc("./", "{*.hoge}") as $file_name) {
echo $file_name . "\r\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment