开发者

How can I glob a directory, sort the files by time/date, and print the names of the files in order?

I'm looking for a way to glob a directory and sort the contents by time/date and print it on the PHP page. There must be a way to do this, I've tried the following code but it won't print anything out on the page:

<?php
$files = glob("subdir/*");
$files = array_combine($files, array_map("filemtime", $files));
arsort($files);
?>

print_r wont work because 开发者_开发知识库I need just the file name. I'm new to PHP arrays so I need as much help as I can get!


Given your original code:

$files = glob("subdir/*");
$files = array_combine($files, array_map("filemtime", $files));
arsort($files);

From there you could either loop on the array of sorted filename/mtime pairs, or create a new array with just the filenames (in their sorted order).

The first looks like:

foreach ($files as $file => $mtime) {
    echo $file . " ";
}

The second could be:

foreach (array_keys($files) as $file) {
    echo $file . " ";
}

Depending on your needs, it might also be okay to simply:

echo implode(" ", array_keys($files));


Sounds like you're looking for foreach

foreach($files as $filename=>$mtime){
    echo AS INTENDED;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜