Mercurial > dirlist-php
changeset 3:9ea8be5b28d1
"refactor".
author | edogawaconan <me@myconan.net> |
---|---|
date | Tue, 21 Oct 2014 18:01:43 +0900 |
parents | 9b5884ba214a |
children | 7812af1c23ae |
files | index.php |
diffstat | 1 files changed, 108 insertions(+), 32 deletions(-) [+] |
line wrap: on
line diff
--- a/index.php Tue Oct 21 15:31:30 2014 +0900 +++ b/index.php Tue Oct 21 18:01:43 2014 +0900 @@ -24,7 +24,7 @@ function link_to($target, $title) { return("<a href=\"".a($target)."\">".h($title)."</a>"); } - function print_header() { + function path_tree_header() { global $link_prefix, $link_base, $to_dir; $path_tree = link_to('/', '[root]') . "/"; foreach ($to_dir as $level => $dir) { @@ -34,54 +34,130 @@ $path_tree .= link_to($link, $dir)."/"; } } - header('Content-Type: text/html; charset=utf-8'); - echo "<!doctype html> - <meta http-equiv=\"Content-type\" content=\"text/html;charset=UTF-8\"> - <title>Index of ", h(rtrim($link_base,"/")."/"), "</title> - <body> - <h1>Index of $path_tree</h1>"; + return "Index of $path_tree"; + } + function title() { + global $link_base; + return "Index of ".h(rtrim($link_base,"/")."/"); } + function nice_size($size) { - static $unit = array('B', 'KB', 'MB', 'GB', 'TB'); - for ($i = 0; $size >= 1000 && $i < count($unit); $i++) { $size /= 1000; } - if ($i == 0) { return(sprintf("%d %s", $size, $unit[$i])); } - else { return(sprintf("%.2f %s", $size, $unit[$i])); } + $thousand_units = array('ko', 'Mo', 'Go', 'To', 'Po'); + + $return_format = "%d %s"; + + if ($size <= 1) { + $return_unit = "octet"; + } elseif ($size < 10000) { + $return_unit = "octets"; + } else { + $size /= 1000; + for ($i = 0; $size >= 1000 && $i < count($thousand_units); $i++) { $size /= 1000; } + $return_format = "%.2f %s"; + $return_unit = $thousand_units[$i]; + } + return sprintf($return_format, $size, $return_unit); } + function file_rows($files, $is_dir) { global $path, $link_base, $link_prefix; - static $unit = array('B', 'KB', 'MB', 'GB', 'TB'); + $file_rows = ""; if ($is_dir) { $file_suffix = "/"; if($link_base != $link_prefix) { - $file_rows .= "<tr><td colspan=3>".link_to(dirname($link_base),"..")."</td></tr>"; + $file_rows .= "<tr><td colspan=3>".link_to(dirname($link_base)."/","..")."</td></tr>"; } } else { $file_suffix = ""; } + foreach($files as $file) { $file_stat = stat("$path/".$file); - $file_rows .= "<tr><td>". - link_to("$link_base/".$file, $file.$file_suffix)."</td><td>". - h(strftime("%a %d-%b-%G %H:%M", $file_stat['mtime']))."</td><td>"; + + $file_rows .= "<tr>"; + $file_rows .= "<td>".link_to("$link_base/".$file.$file_suffix, $file.$file_suffix)."</td>"; + + $file_rows .= "<td>"; if ($is_dir) { $file_rows .= "[dir]"; } else { $file_rows .= nice_size($file_stat['size']); } + $file_rows .= "</td>"; + + $file_rows .= "<td>".h(strftime("%Y-%m-%d %H:%M %Z", $file_stat['mtime']))."</td>"; + $file_rows .= "</tr>"; } return $file_rows; } - function print_list() { - global $dirs, $files; - echo "<table><thead><tr>", - "<td>File</td>", - "<td>Date</td>", - "<td>Size</td>", - "</tr></thead>", - "<tbody>", file_rows($dirs, true), file_rows($files, false), "</tbody>", - "</table>"; - } - function print_footer() { - echo "</body>"; - } - print_header(); - print_list(); - print_footer(); ?> +<?php header('Content-Type: text/html; charset=utf-8'); ?> +<!doctype html> +<head> + <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> + <title><?php echo title(); ?></title> + <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> + <style type="text/css"> + * { box-sizing: border-box; } + body { + font-family: Segoe UI, sans-serif; + font-size: 14px; + } + h1 { margin: 5px; } + table { + width: 100%; + } + th:first-child, td:first-child { + width: 100%; + white-space: pre-wrap; + word-wrap: break-word; + word-break: break-all; + } + tr { + position: relative; + } + th, td { + white-space: nowrap; + padding: 2px 5px; + } + + @media (min-width: 768px) { + th { background: #ccc; } + tr:nth-child(even) { background: #eee; } + tr:hover { background: #ddd; } + } + + @media (max-width: 767px) { + table { + border-spacing: 0 10px; + } + th { display: none; } + tr { + background: #eee; + } + td { + display: inline-block; + } + td:first-child { + background: #ddd; + padding: 5px; + } + table a { + font-size: 18px; + display: block; + } + } + </style> +</head> +<body> + <h1><?php echo path_tree_header(); ?></h1> + + <table> + <thead><tr> + <th>File</th> + <th>Size</th> + <th>Date</th> + </tr></thead> + <tbody> + <?php echo file_rows($dirs, true); ?> + <?php echo file_rows($files, false); ?> + </tbody> + </table> +</body>