Skip to content

Instantly share code, notes, and snippets.

@rodneyrehm
Created June 19, 2011 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rodneyrehm/1034257 to your computer and use it in GitHub Desktop.
Save rodneyrehm/1034257 to your computer and use it in GitHub Desktop.
`ls -alhR` for the SSH-deprived
<?php
if (!empty($argv[1])) {
// directory given by CLI argument: list.php /foo/bar
$dir = $argv[1];
} elseif (!empty($_GET['dir'])) {
// directory given by HTTP: /list.php?dir=/foo/bar
$dir = $_GET['dir'];
} else {
// no directory given, use the one list.php is in
$dir = dirname(__FILE__);
}
if (!file_exists($dir) || !is_dir($dir)) {
throw new Exception("Cannot open directory '{$dir}'");
}
if (PHP_SAPI != 'cli') {
// we're sending plain text…
header('Content-Type: text/plain; charset=UTF-8');
}
$format = "%-10s %-8s %-8s %10s %-10s %s\n";
printf($format,
"Perm.",
"Owner",
"Group",
"Size",
"Created",
"File"
);
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
foreach ($iterator as $file) {
// readable permissions
$perms = readable_permissions($file->getPerms());
// readable Size
$sizes = array('B', 'K', 'M', 'G');
$_size = 0;
$size = $file->getSize();
while ($size > 1024) {
$_size++;
$size /= 1024;
}
$size = sprintf("%0.2f%s", $size, $sizes[$_size]);
// owner name
if (is_callable('posix_getpwuid')) {
$t = posix_getpwuid($file->getOwner());
$owner = $t['name'];
} else {
$owner = $file->getOwner();
}
// group name
if (is_callable('posix_getgrgid')) {
$t = posix_getgrgid($file->getGroup());
$group = $t['name'];
} else {
$group = $file->getGroup();
}
// print…
printf($format,
$perms,
$owner,
$group,
$size,
date('d.m.Y', $file->getCtime()),
$file->getPathname()
);
}
function readable_permissions($perms) {
// http://php.net/manual/en/function.fileperms.php#example-1942
if (($perms & 0xC000) == 0xC000) {
// Socket
$info = 's';
} elseif (($perms & 0xA000) == 0xA000) {
// Symbolic Link
$info = 'l';
} elseif (($perms & 0x8000) == 0x8000) {
// Regular
$info = '-';
} elseif (($perms & 0x6000) == 0x6000) {
// Block special
$info = 'b';
} elseif (($perms & 0x4000) == 0x4000) {
// Directory
$info = 'd';
} elseif (($perms & 0x2000) == 0x2000) {
// Character special
$info = 'c';
} elseif (($perms & 0x1000) == 0x1000) {
// FIFO pipe
$info = 'p';
} else {
// Unknown
$info = 'u';
}
// Owner
$info .= (($perms & 0x0100) ? 'r' : '-');
$info .= (($perms & 0x0080) ? 'w' : '-');
$info .= (($perms & 0x0040) ?
(($perms & 0x0800) ? 's' : 'x' ) :
(($perms & 0x0800) ? 'S' : '-'));
// Group
$info .= (($perms & 0x0020) ? 'r' : '-');
$info .= (($perms & 0x0010) ? 'w' : '-');
$info .= (($perms & 0x0008) ?
(($perms & 0x0400) ? 's' : 'x' ) :
(($perms & 0x0400) ? 'S' : '-'));
// World
$info .= (($perms & 0x0004) ? 'r' : '-');
$info .= (($perms & 0x0002) ? 'w' : '-');
$info .= (($perms & 0x0001) ?
(($perms & 0x0200) ? 't' : 'x' ) :
(($perms & 0x0200) ? 'T' : '-'));
return $info;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment