PHP: Last file in directory
H开发者_Go百科ow do I get the name of the last file (alphabetically) in a directory with php? Thanks.
Using the Directories extension you can do it simply with
$all_files = scandir("/my/path",1);
$last_files = $all_files[0];
The scandir command returns an array with the list of files in a directory. The second parameter specifies the sort order (defaults to ascending, 1 for descending).
<?php
$dir = '/tmp';
$files = scandir($dir, 1);
$last_file = $files[0];
print($last_file);
?>
Code here looks like it would help - would just need to use end($array) to collect the last value in the generated array.
$files = scandir('path/to/dir');
sort($files, SORT_LOCALE_STRING);
array_pop($files);
精彩评论