<?php
header('Content-Type: text/plain');
function ls($path, $hidden_files = true, $dir_size = true, $descending_order = false){
if(!is_dir($path)){
return false;
}
$descending_order = (int) $descending_order;
$files = scandir($path, $descending_order);
unset($files[0], $files[1]);
chdir($path);
$ls = array();
clearstatcache();
foreach($files as $filename){
if(!$hidden_files && substr($filename, 0, 1) == '.'){
continue;
}
$file = array(
'name' => $filename,
'full_path' => realpath($filename)
);
$perms = fileperms($filename);
if (($perms & 0xC000) == 0xC000) {
$info = 's';
} elseif (($perms & 0xA000) == 0xA000) {
$info = 'l';
} elseif (($perms & 0x8000) == 0x8000) {
$info = '-';
} elseif (($perms & 0x6000) == 0x6000) {
$info = 'b';
} elseif (($perms & 0x4000) == 0x4000) {
$info = 'd';
} elseif (($perms & 0x2000) == 0x2000) {
$info = 'c';
} elseif (($perms & 0x1000) == 0x1000) {
$info = 'p';
} else {
$info = 'u';
}
$info .= (($perms & 0x0100) ? 'r' : '-');
$info .= (($perms & 0x0080) ? 'w' : '-');
$info .= (($perms & 0x0040) ?
(($perms & 0x0800) ? 's' : 'x' ) :
(($perms & 0x0800) ? 'S' : '-'));
$info .= (($perms & 0x0020) ? 'r' : '-');
$info .= (($perms & 0x0010) ? 'w' : '-');
$info .= (($perms & 0x0008) ?
(($perms & 0x0400) ? 's' : 'x' ) :
(($perms & 0x0400) ? 'S' : '-'));
$info .= (($perms & 0x0004) ? 'r' : '-');
$info .= (($perms & 0x0002) ? 'w' : '-');
$info .= (($perms & 0x0001) ?
(($perms & 0x0200) ? 't' : 'x' ) :
(($perms & 0x0200) ? 'T' : '-'));
$file['permissions'] = substr(sprintf('%o', $perms), -4);
$file['full_permissions'] = $info;
$owner = posix_getpwuid(fileowner($filename));
$group = posix_getgrgid(filegroup($filename));
$file['owner_group'] = $owner['name'] . ':' . $group['name'];
if(is_dir($filename)){
$file['type'] = 'dir';
$total_size = 0;
if($dir_size){
$original_dir = getcwd();
chdir($filename);
foreach(scandir('.') as $subfile){
$total_size += filesize($subfile);
}
chdir($original_dir);
unset($original_dir);
}
$file['size'] = $total_size;
}else {
$file['type'] = 'file';
$file['size'] = filesize($filename);
}
$size = $file['size'];
$units = array(
'B',
'KB',
'MB',
'GB',
'TB'
);
for ($i = 0; $size > 1024; $i++) {
$size /= 1024;
}
$file['human_size'] = round($size, 2) . ' ' . $units[$i];
if(!function_exists('mime_content_type')){
$mime_types = array(
'txt' => 'text/plain',
'htm' => 'text/html',
'html' => 'text/html',
'php' => 'text/html',
'css' => 'text/css',
'js' => 'application/javascript',
'json' => 'application/json',
'xml' => 'application/xml',
'swf' => 'application/x-shockwave-flash',
'flv' => 'video/x-flv',
'png' => 'image/png',
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'ico' => 'image/vnd.microsoft.icon',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'svg' => 'image/svg+xml',
'svgz' => 'image/svg+xml',
'zip' => 'application/zip',
'rar' => 'application/x-rar-compressed',
'exe' => 'application/x-msdownload',
'msi' => 'application/x-msdownload',
'cab' => 'application/vnd.ms-cab-compressed',
'mp3' => 'audio/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
'pdf' => 'application/pdf',
'psd' => 'image/vnd.adobe.photoshop',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
'doc' => 'application/msword',
'rtf' => 'application/rtf',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'odt' => 'application/vnd.oasis.opendocument.text',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
);
$ext = strtolower(array_pop(explode('.',$filename)));
if (array_key_exists($ext, $mime_types)) {
return $mime_types[$ext];
}
elseif (function_exists('finfo_open')) {
$finfo = finfo_open(FILEINFO_MIME);
$mimetype = finfo_file($finfo, $filename);
finfo_close($finfo);
return $mimetype;
}
else {
return 'application/octet-stream';
}
}else {
$file['mimetype'] = mime_content_type($filename);
}
if(substr($file['mimetype'], 0, 5) == 'image'){
$dimensions = getimagesize($filename);
if($dimensions !== false){
$file['dimensions'] = array(
'width' => $dimensions[0],
'height' => $dimensions[1]
);
}
}
$stat = stat($filename);
$filemtime = $stat['mtime'];
$fileatime = $stat['atime'];
$file['last_modified'] = $filemtime;
$file['human_last_modified'] = date ("F d Y H:i:s", $filemtime);
$file['last_accessed'] = $fileatime;
$file['human_last_accessed'] = date ("F d Y H:i:s", $fileatime);
$ls[] = $file;
}
return $ls;
}
print_r(ls('./'));
?>