0

I want to make a small extra menu for my website. Is it possible to get the name of the files in a directory and put them in the menu.

So if you have a directory with files: facebook.php; twitter.php; stackoverflow.html; that you will get a menu like this:

  • facebook
  • twitter
  • stackoverflow

And if it's possible I want to choose that what kind of files he gets. So I want him to get the name of .php and .html files but not of .css files.

Can somebody help me with this?

Moreno__R
  • 45
  • 6
  • This question has been asked before - http://stackoverflow.com/questions/8541180/best-way-to-get-files-from-a-dir-filtered-by-certain-extension-in-php – Terry Oct 12 '13 at 15:28
  • http://php.net/manual/en/function.scandir.php – ultranaut Oct 12 '13 at 15:33

2 Answers2

3

I like glob():

foreach(glob("$dir/{*.php,*.html}", GLOB_BRACE) as $file) {
    //whatever
}

or

$files = glob("$dir/{*.php,*.html}", GLOB_BRACE); //then use $files wherever

You can use pathinfo() with PATHINFO_BASENAME to get only filename PATHINFO_FILENAME to get it without the extension.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • Now i know how to get the file names, but do you also know how i can put each filename in a other li? I'm not really good with php but I want to learn it. – Moreno__R Oct 12 '13 at 20:47
  • 1
    @Moreno__R: In the first example just replace //whatever with `echo "
  • ".pathinfo($file, PATHINFO_FILENAME)."
  • ";` – AbraCadaver Oct 13 '13 at 00:09