PHP: last file in directory

How to get the name of the last file (in alphabetical order) in a directory with php? Thank.

+3
source share
4 answers

Using the Directories extension , you can do this simply with

$all_files = scandir("/my/path",1);
$last_files = $all_files[0];
+8
source

The scandir command returns an array with a list of files in the directory. The second parameter indicates the sort order (default is ascending, 1 for descent).

<?php
$dir    = '/tmp';
$files = scandir($dir, 1);
$last_file = $files[0];
print($last_file);
?>
+2
source

The code looks like it will help - you just need to use end ($ array) to collect the last value in the generated array.

0
source
$files = scandir('path/to/dir');
sort($files, SORT_LOCALE_STRING);
array_pop($files);
0
source

Source: https://habr.com/ru/post/1729142/


All Articles