現在の言語: 日本語

戻る

ファイル/フォルダ
フォルダ関連

再帰処理を利用してフォルダリストの
フォルダ
ファイル
を取得します。

[サンプル]

copy
$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
*/
copy
$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
*/
$files = scandir($dir);
指定したパスのフォルダおよびファイルの一覧を取得します。
この一覧からフォルダかファイルを判定し
フォルダの場合は、その中のリストを再帰処理を使用して取得します。
この処理方法により、指定したパスの全のフォルダパスおよびファイルパスを取得する方法となります。

DIRECTORY_SEPARATOR
Windowsではパスの区切りに \ を使い、LinuxやMacでは / を使います。
PHPの DIRECTORY_SEPARATOR 定数を使うと、
実行されているOSに合わせて自動的に適切な区切り文字(/ か \)を選択してくれます。
if ($file === '.' || $file === '..') continue;
.
カレントディレクトリ
..
親ディレクトリ
recursionDirectory($path, $aryPath, $aryType);
フォルダの場合は、そのフォルダ内のリストを取得する必要があるため
実行している再帰関数を実行します。
そのとき、第1引数の$pathには
$path = $dir . DIRECTORY_SEPARATOR . $file;
フォルダパスを渡します。


戻る

著作権情報
ホームページおよびアプリ等に掲載されている情報等については、いかなる保障もいたしません。
ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、
または公開したりすることはできません。
当方は、ホームページおよびアプリ等を利用したいかなる理由によっての障害等が発生しても、
その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について
一切の責任を負わないものとします。