Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include JUST files in scandir array?

Tags:

php

scandir

I have an array I'm getting back from scandir, but it contains "." and ".." and I don't want it to.

My code:

$indir = scandir('../pages'); $fileextensions = array(".", "php", "html", "htm", "shtml"); $replaceextensions = str_replace($fileextensions, "", $indir); 

I am doing a string replace on the file extensions, thus causing [0] and [1] to appear empty, but they are "." and ".."

array(4) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(4) "test" [3]=> string(4) "home" } 

How would I remove the "." and ".." from the array?

like image 296
Rbn Avatar asked Feb 04 '13 03:02

Rbn


1 Answers

You can use array_filter.

$indir = array_filter(scandir('../pages'), function($item) {     return !is_dir('../pages/' . $item); }); 

Note this filters out all directories and leaves only files and symlinks. If you really want to only exclude only files (and directories) starting with ., then you could do something like:

$indir = array_filter(scandir('../pages'), function($item) {     return $item[0] !== '.'; }); 
like image 167
leftclickben Avatar answered Sep 20 '22 13:09

leftclickben