現在の言語: 日本語 |
ファイル/フォルダ |
フォルダ関連 |
$aryType = [];//[0]folder [1]file
$aryPath = [];//全てのパスを格納する配列
$dir = ".";
//$dir = "./a";
//$dir = "./b";
recursionDirectory($dir, $aryPath, $aryType);
for($i=0; $i < count($aryPath); ++$i)
{
echo "i:$i type:".$aryType[$i]." path:".$aryPath[$i]."<br>";
}
function recursionDirectory(string $dir, array &$aryPath, array &$aryType)
{
$files = scandir($dir);
foreach ($files as $file)
{
// 「.」と「..」はスキップ
if ($file === '.' || $file === '..') continue;
//macのケース
if($file === ".DS_Store") continue;
//Windowsではパスの区切りに \ を使い、LinuxやMacでは / を使います。
//PHPの DIRECTORY_SEPARATOR 定数を使うと、
//実行されているOSに合わせて自動的に適切な区切り文字(/ か \)を選択してくれます。
$path = $dir . DIRECTORY_SEPARATOR . $file;
$aryPath[] = $path;
if(is_dir($path))//フォルダ
{
$aryType[] = 0;
recursionDirectory($path, $aryPath, $aryType);
}
else//ファイル
{
$aryType[] = 1;
}
}
}
/*
[出力結果]
---------------
$dir = ".";
---------------
i:0 type:0 path:./a
i:1 type:0 path:./a/a
i:2 type:1 path:./a/test1.html
i:3 type:1 path:./a/test2.php
i:4 type:0 path:./b
i:5 type:0 path:./b/c
i:6 type:1 path:./b/c/test2.html
i:7 type:1 path:./b/test2.html
i:8 type:1 path:./b/test2.php
i:9 type:1 path:./test1.php
---------------
$dir = "./a";
---------------
i:0 type:0 path:./a/a
i:1 type:1 path:./a/test1.html
i:2 type:1 path:./a/test2.php
---------------
$dir = "./b";
---------------
i:0 type:0 path:./b/c
i:1 type:1 path:./b/c/test2.html
i:2 type:1 path:./b/test2.html
i:3 type:1 path:./b/test2.php
*/
$aryType = []; //[0]folder [1]file
$aryPath = []; //Array storing all paths
$dir = ".";
//$dir = "./a";
//$dir = "./b";
recursionDirectory($dir, $aryPath, $aryType);
for($i=0; $i < count($aryPath); ++$i)
{
echo "i:$i type:".$aryType[$i]." path:".$aryPath[$i]."<br>";
}
function recursionDirectory(string $dir, array &$aryPath, array &$aryType)
{
$files = scandir($dir);
foreach ($files as $file)
{
/ Skip "." and ".."
if ($file === '.' || $file === '..') continue;
//Mac case
if($file === ".DS_Store") continue;
//Windows uses \ as the path separator, while Linux and Mac use / .
//PHP's DIRECTORY_SEPARATOR constant can be used to automatically select the appropriate separator (/ or \) based on the operating system being used.
$path = $dir . DIRECTORY_SEPARATOR . $file;
$aryPath[] = $path;
if(is_dir($path))//folder
{
$aryType[] = 0;
recursionDirectory($path, $aryPath, $aryType);
}
else//file
{
$aryType[] = 1;
}
}
}
/*
[Output result]
---------------
$dir = ".";
---------------
i:0 type:0 path:./a
i:1 type:0 path:./a/a
i:2 type:1 path:./a/test1.html
i:3 type:1 path:./a/test2.php
i:4 type:0 path:./b
i:5 type:0 path:./b/c
i:6 type:1 path:./b/c/test2.html
i:7 type:1 path:./b/test2.html
i:8 type:1 path:./b/test2.php
i:9 type:1 path:./test1.php
---------------
$dir = "./a";
---------------
i:0 type:0 path:./a/a
i:1 type:1 path:./a/test1.html
i:2 type:1 path:./a/test2.php
---------------
$dir = "./b";
---------------
i:0 type:0 path:./b/c
i:1 type:1 path:./b/c/test2.html
i:2 type:1 path:./b/test2.html
i:3 type:1 path:./b/test2.php
*/
| 著作権情報 |
| ホームページおよびアプリ等に掲載されている情報等については、いかなる保障もいたしません。 ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、 または公開したりすることはできません。 当方は、ホームページおよびアプリ等を利用したいかなる理由によっての障害等が発生しても、 その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について 一切の責任を負わないものとします。 |