Please see the following function to scan the files in a directory (Taken from here)
function scandir_only_files($dir) {
return array_filter(scandir($dir), function ($item) {
return is_file($dir.DIRECTORY_SEPARATOR.$item);
});
}
This does not work because the $dir is not in scope in the anonymous function, and shows up empty, causing the filter to return FALSE every time. How would I rewrite this?
You have to explicitly declare variables inherited from the parent scope, with the
usekeyword:See this example from the anonymous functions page.