简体   繁体   中英

PHP readir results - trying to sort by date created and also get rid of “.” and “..”

I have a double question. Part one: I've pulled a nice list of pdf files from a directory and have appended a file called download.php to the "href" link so the pdf files don't try to open as a web page (they do save/save as instead). Trouble is I need to order the pdf files/links by date created. I've tried lots of variations but nothing seems to work! Script below. I'd also like to get rid of the "." and ".." directory dots! Any ideas on how to achieve all of that. Individually, these problems have been solved before, but not with my appended download.php scenario :)

<?php

$dir="../uploads2"; // Directory where files are stored


if ($dir_list = opendir($dir))
{
while(($filename = readdir($dir_list)) !== false)


{
?>
<p><a href="http://www.duncton.org/download.php?file=login/uploads2/<?php echo $filename; ?>"><?php echo $filename;
?></a></p>
<?php


}
closedir($dir_list);
}

?>

While you can filter them out * , the . and .. handles always come first. So you could just cut them away. In particular if you use the simpler scandir() method:

foreach (array_slice(scandir($dir), 2) as $filename) {

One could also use glob("dir/*") which skips dotfiles implicitly. As it returns the full path sorting by ctime then becomes easier as well:

$files = glob("dir/*");

// make filename->ctime mapping
$files = array_combine($files, array_map("filectime", $files));

// sorts filename list
arsort($files);
$files = array_keys($files);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM