Skip to content

Instantly share code, notes, and snippets.

@k-holy
Created October 19, 2012 07:42
Show Gist options
  • Save k-holy/3916763 to your computer and use it in GitHub Desktop.
Save k-holy/3916763 to your computer and use it in GitHub Desktop.
SplFileInfoのメソッド調査
<?php
namespace Acme;
class U
{
public static function H($data, $default=null)
{
$var = $default;
if (isset($data)) {
if (is_bool($data)) {
$var = ($data) ? 'TRUE' : 'FALSE';
} else {
if (strncasecmp(PHP_OS, 'WIN', 3) === 0) {
$data = mb_convert_encoding($data, 'UTF-8', 'SJIS-win');
}
$var = htmlspecialchars($data, ENT_QUOTES, 'UTF-8');
}
}
return $var;
}
}
$files = array();
// 絶対パスのファイル
$files[] = new \SplFileInfo(__FILE__);
// 絶対パスのディレクトリ
$files[] = new \SplFileInfo(__DIR__);
// 相対パスのファイル
$files[] = new \SplFileInfo('../www/splfileinfo.test.php');
// 相対パスのディレクトリ
$files[] = new \SplFileInfo('../www/');
// 存在しないファイル
$files[] = new \SplFileInfo('/path/to/file.php');
// 存在しないディレクトリ
$files[] = new \SplFileInfo('/path/to/');
// 非ASCII文字のファイル
$path = __DIR__ . DIRECTORY_SEPARATOR . '日本語.txt';
if (strncasecmp(PHP_OS, 'WIN', 3) === 0) {
$path = mb_convert_encoding($path, 'SJIS-win', 'UTF-8');
}
$files[] = new \SplFileInfo($path);
// 非ASCII文字のディレクトリをパスに含むファイル
$path = __DIR__ . DIRECTORY_SEPARATOR . '日本語' . DIRECTORY_SEPARATOR . 'test.txt';
if (strncasecmp(PHP_OS, 'WIN', 3) === 0) {
$path = mb_convert_encoding($path, 'SJIS-win', 'UTF-8');
}
$files[] = new \SplFileInfo($path);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="robots" content="noindex,nofollow" />
<style type="text/css">
body {font-family:monospace;}
table {
width:80%;
border-collapse:collapse;
margin:10px 0px;
}
caption {
text-align:left;
font-size:120%;
font-weight:bold;
}
th, td {
text-align:left;
padding:2px;
border:solid 1px #999999;
}
th {
width:25em;
}
</style>
<title>SplFileInfo @<?=U::H($_SERVER['HTTP_HOST'])?></title>
</head>
<body>
<?php foreach ($files as $file) : ?>
<table>
<caption><?=U::H((string)$file)?></caption>
<tr>
<th>SplFileInfo::getPathname()</th>
<td><?=U::H($file->getPathname())?></td>
</tr>
<tr>
<th>SplFileInfo::getPath()</th>
<td><?=U::H($file->getPath())?></td>
</tr>
<tr>
<th>SplFileInfo::getFilename()</th>
<td><?=U::H($file->getFilename())?></td>
</tr>
<tr>
<th>SplFileInfo::getBasename()</th>
<td><?=U::H($file->getBasename())?></td>
</tr>
<tr>
<th>SplFileInfo::getExtension()</th>
<td><?=U::H($file->getExtension())?></td>
</tr>
<tr>
<th>dirname($file)</th>
<td><?=U::H(dirname((string)$file))?></td>
</tr>
<tr>
<th>basename($file)</th>
<td><?=U::H(basename((string)$file))?></td>
</tr>
<tr>
<th>pathinfo($file, PATHINFO_DIRNAME)</th>
<td><?=U::H(pathinfo((string)$file, PATHINFO_DIRNAME))?></td>
</tr>
<tr>
<th>pathinfo($file, PATHINFO_BASENAME)</th>
<td><?=U::H(pathinfo((string)$file, PATHINFO_BASENAME))?></td>
</tr>
<tr>
<th>pathinfo($file, PATHINFO_FILENAME)</th>
<td><?=U::H(pathinfo((string)$file, PATHINFO_FILENAME))?></td>
</tr>
<tr>
<th>pathinfo($file, PATHINFO_EXTENSION)</th>
<td><?=U::H(pathinfo((string)$file, PATHINFO_EXTENSION))?></td>
</tr>
<tr>
<th>SplFileInfo::getRealPath()</th>
<td><?=U::H($file->getRealPath())?></td>
</tr>
<tr>
<th>SplFileInfo::getType()</th>
<td><?php try{ echo U::H($file->getType()); }catch(\RuntimeException $e){ echo U::H($e->getMessage());}?></td>
</tr>
<tr>
<th>SplFileInfo::isDir()</th>
<td><?=U::H($file->isDir())?></td>
</tr>
<tr>
<th>SplFileInfo::isFile()</th>
<td><?=U::H($file->isFile())?></td>
</tr>
<tr>
<th>SplFileInfo::isReadable()</th>
<td><?=U::H($file->isReadable())?></td>
</tr>
<tr>
<th>SplFileInfo::getPathInfo()</th>
<td><?=U::H((string)$file->getPathInfo())?></td>
</tr>
<tr>
<th>SplFileInfo::__toString()</th>
<td><?=U::H((string)$file)?></td>
</tr>
</table>
<?php endforeach ?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment