datetime - PHP Sorting files by Date/time and file size -
i working on modifying simple website. website has page shows clients files available download(if applicable). @ moment these files in random order no specific details. able have them in decending order based on time stamp made available. including file size. using php show files, need have directory sorted before displaying them? if separate script , when run? or can sort them displayed in follwoing code?
<div id="f2"> <h3>files available download</h3> <p> <?php // list contents of user directory if (file_exists($user_directory)) { if ($handle = opendir($user_directory)) { while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != "..") { echo "<a href='download_file.php?filename=".urlencode($entry)."'>".$entry."</a><br/>"; } } closedir($handle); } } ?> </p> </div>
very new php, appreciated.
here's snippet of help.
it makes href
out of each file or file extension specified. modify suit.
- it presently coded use
asort()
function. consult php manual.
it can changed usort()
. consult php manual.
see arsort()
function. consult php manual.
the filesize included, yet not formatted bytes, kb etc. there functions out there format suit. google "filesize format php". this link has information.
<?php // can use desired folder check , comment others. // foreach (glob("../downloads/*") $path) { // lists files in sub-folder called "downloads" foreach (glob("test/*") $path) { // lists files in folder called "test" //foreach (glob("*.php") $path) { // lists files .php extension in current folder $docs[$path] = filectime($path); } asort($docs); // sort value, preserving keys foreach ($docs $path => $timestamp) { print date("d m. y: ", $timestamp); print '<a href="'. $path .'">'. basename($path) .'</a>' . " size: " . filesize($path) .'<br />'; } ?>
pulled link http://codebyte.dev7studios.com/post/1590919646/php-format-filesize, should ever cease exist:
function filesize_format($size, $sizes = array('bytes', 'kb', 'mb', 'gb', 'tb', 'pb', 'eb', 'zb', 'yb')) { if ($size == 0) return('n/a'); return (round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . ' ' . $sizes[$i]); }
Comments
Post a Comment