Created
May 30, 2012 10:42
-
-
Save k-holy/2835436 to your computer and use it in GitHub Desktop.
RecursiveIteratorIterator+FileInfo関数でディレクトリ内のファイルから再帰的にMIME-Typeを取得
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="ja"> | |
<head> | |
<meta charset="UTF-8" /> | |
</head> | |
<body> | |
<ul> | |
<li> | |
<dl> | |
<dt>cab.cab</dt> | |
<dd>application/vnd.ms-cab-compressed</dd> | |
</dl> | |
</li> | |
<li> | |
<dl> | |
<dt>csv.csv</dt> | |
<dd>text/plain</dd> | |
</dl> | |
</li> | |
<li> | |
<dl> | |
<dt>exe.jpg</dt> | |
<dd>application/x-dosexec</dd> | |
</dl> | |
</li> | |
<li> | |
<dl> | |
<dt>index.php</dt> | |
<dd>text/x-c++</dd> | |
</dl> | |
</li> | |
<li> | |
<dl> | |
<dt>jpeg.exe</dt> | |
<dd>image/jpeg</dd> | |
</dl> | |
</li> | |
<li> | |
<dl> | |
<dt>jpeg.jpg</dt> | |
<dd>image/jpeg</dd> | |
</dl> | |
</li> | |
<li> | |
<dl> | |
<dt>magic</dt> | |
<dd>text/plain</dd> | |
</dl> | |
</li> | |
<li> | |
<dl> | |
<dt>png.png</dt> | |
<dd>image/png</dd> | |
</dl> | |
</li> | |
<li> | |
<dl> | |
<dt>xls.xls</dt> | |
<dd>application/vnd.ms-excel</dd> | |
</dl> | |
</li> | |
</ul> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme; | |
class U { | |
public static function H($data, $default = null) { | |
if (isset($data)) { | |
return htmlspecialchars($data, ENT_QUOTES, 'UTF-8'); | |
} | |
return $default; | |
} | |
} | |
/** | |
* ファイル名によるソート用イテレータ | |
* via http://stackoverflow.com/questions/2930405/sort-directory-listing-using-recursivedirectoryiterator | |
*/ | |
class SortIterator extends \SplHeap | |
{ | |
public function __construct(\Iterator $iterator) | |
{ | |
foreach ($iterator as $item) { | |
$this->insert($item); | |
} | |
} | |
public function compare($b, $a) | |
{ | |
return strcmp($a->getFilename(), $b->getFilename()); | |
} | |
} | |
// ディレクトリから再帰的にファイルを取得 | |
$iterator = new SortIterator( | |
new \RecursiveIteratorIterator( | |
new \RecursiveDirectoryIterator(__DIR__, | |
\FilesystemIterator::CURRENT_AS_FILEINFO | | |
\FilesystemIterator::KEY_AS_PATHNAME | | |
\FilesystemIterator::SKIP_DOTS | |
), | |
\RecursiveIteratorIterator::LEAVES_ONLY | |
) | |
); | |
$getMimeTypeFrom = new \finfo(FILEINFO_MIME_TYPE); | |
?> | |
<!DOCTYPE html> | |
<html lang="ja"> | |
<head> | |
<meta charset="UTF-8" /> | |
</head> | |
<body> | |
<ul> | |
<?php foreach ($iterator as $fileInfo) : ?> | |
<li> | |
<dl> | |
<dt><?=U::H($fileInfo->getFilename())?></dt> | |
<dd><?=U::H($getMimeTypeFrom->file($fileInfo->getRealpath()))?></dd> | |
</dl> | |
</li> | |
<?php endforeach ?> | |
</ul> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment