php - Array slice doesn't seems to work -


i'm trying have first file in directory ordered name asc. here code use (php):

 $dir = "fichiers/123/files_backup"; $premfic = array_slice(array_filter(scandir($dir), 'is_file'), 0, 5); print_r($premfic); 

but array empty... directory contains 18 files , scandir alone sees them. idea? thanks

is_file won't work, because you're not in "fichiers/123/files_backup". following should work:

chdir($dir); $premfic = array_slice(array_filter(scandir('.'), 'is_file'), 0, 5); // may want chdir previous directory // can use getcwd() before chdir() dynamically determine 

or:

$premfic = array_slice(array_filter(scandir($dir), function($filename) use ($dir){     return is_file($dir . '/' . $filename); }), 0, 5); 

or orangepill pointed out (since glob returns relative path, not filename):

$premfic = array_slice(array_filter(glob($dir . '/*'), 'is_file'), 0, 5); 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -