Usage scandir():
array_slice(array_filter(scandir('/path/to/dir/'), 'is_file'), 0, 5);
array_filter()Together with the function callback is_file(), it ensures that we simply process the files without having to write a loop, we don’t even need to take care of .and .., since they are directories.
Or usingglob() - it will not match file names, for example .htaccess:
array_slice(glob('/path/to/dir/*.*'), 0, 5);
glob() + array_filter() - , .htaccess:
array_slice(array_filter(glob('/path/to/dir/*'), 'is_file'), 0, 5);