Ali*_*xel 20
使用scandir():
array_slice(array_filter(scandir('/path/to/dir/'), 'is_file'), 0, 5);
Run Code Online (Sandbox Code Playgroud)
在array_filter()与一起is_file()回调函数可以确保我们只处理文件,而无需编写一个循环,我们甚至没有关心.和..,因为他们是目录.
或使用glob() - 它将不匹配文件名,如.htaccess:
array_slice(glob('/path/to/dir/*.*'), 0, 5);
Run Code Online (Sandbox Code Playgroud)
或使用glob()+array_filter() - 这个将匹配文件名,如.htaccess:
array_slice(array_filter(glob('/path/to/dir/*'), 'is_file'), 0, 5);
Run Code Online (Sandbox Code Playgroud)