Webspace Directory per filezise - wie die Werte formatieren? - Olaf_A - 28.02.2019
Ich habe in meinen Forum ein eigenes Uploadscript eingebaut. Dadurch kann ich z.Bsp. ohne weiteres eine Bildergalerie erstellen und diese für jedes Mitglied im Profil verlinken.
Selbiges mache ich mit den anderen Dateien, welche sich allerdings in einen gesonderten Userverzeichnis befinden.
Das ganze sieht dann so aus:
http://olaf-asmus.de/test/
Hier werden die Dateien auch in KB, MB, GB dargestellt. Ich möchte allerdings nicht KB oder MB ausgeben, sondern immer nur Bytes in einen speziellen Format:
http://olaf-asmus.de/test/0-directory_in_byte.php
Das ganze funktionier ohne Probleme, nur möchte ich den Bytes gerne TAUSENDER Punkte zur besseren Übersichtlichkeit verpassen.
Ich habe dazu in den Verzeichnis die Datei 0-directory_in_byte.zip eingestellt, welche man laden kann.
Kann mir jemand helfen?
MfG Olaf
RE: Webspace Directory per filezise - wie die Werte formatieren? - itsmeJAY - 28.02.2019
Moin,
bin mit Handy online und kann mir die php Datei nicht angucken. Wie sieht's aus mit:
number_format();
http://php.net/manual/de/function.number-format.php
Klappt nicht?
Grüße
JAY
RE: Webspace Directory per filezise - wie die Werte formatieren? - Olaf_A - 28.02.2019
Genau dort habe ich gelesen, dazu noch dieses: http://php.net/manual/de/function.filesize.php
Ich komme da nicht weiter.
Hier mal der Quellcode von meiner PHP Datei:
PHP-Code: <?php date_default_timezone_set('Europe/Berlin'); define('HOST',$_SERVER['HTTP_HOST']); define('URI',$_SERVER['REQUEST_URI']); $files = array(); $dirs = array(); $elements = scandir(getcwd());
foreach($elements as $key => $element) { if($element == 'index.php') continue; $ext = explode('.',$element); $icon = $ext[count($ext)-1]; empty($icon) || count($ext) < 2 ? $icon = 'folder' : null;
if(filetype($element) == 'dir') { $dirs[$key]['name'] = $element; $dirs[$key]['type'] = '<img src="http://olaf-asmus.de/index_icons/'.$icon.'.png" alt="'.strtoupper($icon).'" title="'.strtoupper($icon).'" />'; } else { $files[$key]['name'] = $element; $files[$key]['type'] = '<img src="http://olaf-asmus.de/index_icons/'.$icon.'.png" alt="'.strtoupper($icon).'" title="'.strtoupper($icon).'" />'; } }
function fsize($bytes, $precision = 2) { $units = array('Byte'); $bytes = max($bytes, 0); $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); $pow = min($pow, count($units) - 1); $bytes /= pow(1024, $pow);
return round($bytes, $precision) . ' ' . $units[$pow]; } ?> <!DOCTYPE html> <html> <head> <title>Index of <?= HOST.URI; ?></title> <style type="text/css"> body { font-family: Verdana, Arial, Sans-Serif; font-size: 12pt; color: 422800; background: #eeeeee;}
a:link {color: #422800; text-decoration: none; letter-spacing:0.1em;}
a:hover {color: #002200; text-decoration: underline overline;}
.zui-table { border: solid 1px #2B1A00; border-collapse: collapse; border-spacing: 0; background: #FFF5E6; } .zui-table thead th { background-color: #fbcd89; border: solid 2px #2B1A00; color: #221100; padding: 10px 5px 10px 5px;
} .zui-table tbody td { border: solid 1px #2B1A00; padding: 5px; text-shadow: 1px 1px 1px #666666; } .zui-table-highlight tbody tr:hover { background-color: #fbcd89; } .zui-table-horizontal tbody td { border-left: none; border-right: none; }
.tleft { text-align: left;}
.tcenter { text-align: center; }
.tright { text-align: right; }
</style> </head> <body> <h3>Seite: <?= HOST.URI; ?></h3><hr /> <table class="zui-table zui-table-horizontal zui-table-highlight" cellpadding="0" cellspacing="0" border="0" style="width:1200px;"><thead> <tr> <th class="tcenter" style="width:80px;">Typ</th> <th class="tleft">Name</th> <th class="tright">Groesse</th> <th class="tright">zuletzt bearbeitet</th> </tr> </thead> <tbody> <tr> <td colspan="4"></td> </tr> <?php foreach($dirs as $key => $dir): ?>
<tr> <td align="center"><?= $dir['type']; ?></td> <td><a href="<?= $dir['name']; ?>"><?= $dir['name']; ?></a></td> <td align="right"></td> <td align="right" nowrap><?= date("Y-m-d - H:i:s",filemtime($dir['name'])); ?></td> </tr> <?php endforeach; ?> <?php foreach($files as $key => $file): ?>
<tr> <td align="center"><?= $file['type']; ?></td> <td><a href="<?= $file['name']; ?>"><?= $file['name']; ?></a></td> <td align="right" nowrap><?= fsize(filesize($file['name']),2); ?></td> <td align="right" nowrap><?= date("Y-m-d - H:i:s",filemtime($file['name'])); ?></td> </tr> <?php endforeach; ?> </tbody> </table><br /> </body> </html>
RE: Webspace Directory per filezise - wie die Werte formatieren? - itsmeJAY - 28.02.2019
(28.02.2019, 08:43)Olaf_A schrieb: Genau dort habe ich gelesen, dazu noch dieses: http://php.net/manual/de/function.filesize.php
Ich komme da nicht weiter.
Hier mal der Quellcode von meiner PHP Datei:
PHP-Code: <?php date_default_timezone_set('Europe/Berlin'); define('HOST',$_SERVER['HTTP_HOST']); define('URI',$_SERVER['REQUEST_URI']); $files = array(); $dirs = array(); $elements = scandir(getcwd());
foreach($elements as $key => $element) { if($element == 'index.php') continue; $ext = explode('.',$element); $icon = $ext[count($ext)-1]; empty($icon) || count($ext) < 2 ? $icon = 'folder' : null;
if(filetype($element) == 'dir') { $dirs[$key]['name'] = $element; $dirs[$key]['type'] = '<img src="http://olaf-asmus.de/index_icons/'.$icon.'.png" alt="'.strtoupper($icon).'" title="'.strtoupper($icon).'" />'; } else { $files[$key]['name'] = $element; $files[$key]['type'] = '<img src="http://olaf-asmus.de/index_icons/'.$icon.'.png" alt="'.strtoupper($icon).'" title="'.strtoupper($icon).'" />'; } }
function fsize($bytes, $precision = 2) { $units = array('Byte'); $bytes = max($bytes, 0); $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); $pow = min($pow, count($units) - 1); $bytes /= pow(1024, $pow);
return round($bytes, $precision) . ' ' . $units[$pow]; } ?> <!DOCTYPE html> <html> <head> <title>Index of <?= HOST.URI; ?></title> <style type="text/css"> body { font-family: Verdana, Arial, Sans-Serif; font-size: 12pt; color: 422800; background: #eeeeee;}
a:link {color: #422800; text-decoration: none; letter-spacing:0.1em;}
a:hover {color: #002200; text-decoration: underline overline;}
.zui-table { border: solid 1px #2B1A00; border-collapse: collapse; border-spacing: 0; background: #FFF5E6; } .zui-table thead th { background-color: #fbcd89; border: solid 2px #2B1A00; color: #221100; padding: 10px 5px 10px 5px;
} .zui-table tbody td { border: solid 1px #2B1A00; padding: 5px; text-shadow: 1px 1px 1px #666666; } .zui-table-highlight tbody tr:hover { background-color: #fbcd89; } .zui-table-horizontal tbody td { border-left: none; border-right: none; }
.tleft { text-align: left;}
.tcenter { text-align: center; }
.tright { text-align: right; }
</style> </head> <body> <h3>Seite: <?= HOST.URI; ?></h3><hr /> <table class="zui-table zui-table-horizontal zui-table-highlight" cellpadding="0" cellspacing="0" border="0" style="width:1200px;"><thead> <tr> <th class="tcenter" style="width:80px;">Typ</th> <th class="tleft">Name</th> <th class="tright">Groesse</th> <th class="tright">zuletzt bearbeitet</th> </tr> </thead> <tbody> <tr> <td colspan="4"></td> </tr> <?php foreach($dirs as $key => $dir): ?>
<tr> <td align="center"><?= $dir['type']; ?></td> <td><a href="<?= $dir['name']; ?>"><?= $dir['name']; ?></a></td> <td align="right"></td> <td align="right" nowrap><?= date("Y-m-d - H:i:s",filemtime($dir['name'])); ?></td> </tr> <?php endforeach; ?> <?php foreach($files as $key => $file): ?>
<tr> <td align="center"><?= $file['type']; ?></td> <td><a href="<?= $file['name']; ?>"><?= $file['name']; ?></a></td> <td align="right" nowrap><?= fsize(filesize($file['name']),2); ?></td> <td align="right" nowrap><?= date("Y-m-d - H:i:s",filemtime($file['name'])); ?></td> </tr> <?php endforeach; ?> </tbody> </table><br /> </body> </html>
Moin,
PHP-Code: return number_format(round($bytes, $precision), 3, ',', '.') . ' ' . $units[$pow];
mit Handy runtergeschrieben. Nicht getestet. Ansonsten gucke ich gleich nochmal.
RE: Webspace Directory per filezise - wie die Werte formatieren? - Olaf_A - 01.03.2019
(28.02.2019, 10:02)itsmeJAY schrieb: Moin,
PHP-Code: return number_format(round($bytes, $precision), 3, ',', '.') . ' ' . $units[$pow];
mit Handy runtergeschrieben. Nicht getestet. Ansonsten gucke ich gleich nochmal.
Es geht, habe allerdings noch etwas geändert, denn 3 Stellen hintern Komma braucht man bei den Bytes sicherlich nicht.
PHP-Code: return number_format(round($bytes, $precision), 0, ',', '.') . ' ' . $units[$pow];
Vielen Dank für die schnelle Hilfe.
MfG Olaf
RE: Webspace Directory per filezise - wie die Werte formatieren? - itsmeJAY - 01.03.2019
.. hatte ich mir fast gedacht das du es änderst
Viel Spaß
Grüße
RE: Webspace Directory per filezise - wie die Werte formatieren? - StefanT - 01.03.2019
Auf das Runden mittels round() kann man an dieser Stelle übrigens verzichten.
RE: Webspace Directory per filezise - wie die Werte formatieren? - itsmeJAY - 01.03.2019
stimmt. Ich hab ja gestern schon gesagt, ich bin ein dussel. Lag wohl an der Uhrzeit früh am morgen
|