开发者

PHP: Last file in directory

开发者 https://www.devze.com 2022-12-17 02:01 出处:网络
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

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);
0

精彩评论

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

关注公众号