tags:

views:

29

answers:

5

How could I use the php function glob to get files from a directory and put them in an array and then include them with index.php?file=filename if it exists?

This is what I came up with so far but it doesn't work.

$files = glob("files/*.php");
$file = array($files);

if (in_array(..

OR is there a smarter way to do this without having to write all pages in the index.php?

A: 

If I'm understanding you right you can use array_intersect():

$files = glob("files/*.php");
$allowedFiles = array('1.php', '2.php', '3.php')

$matchedFiles = array_intersect($files, $allowedFiles);

foreach ($matchedFiles as $file)
{
    // do something with 'index.php?file=' . $file
}

Not sure what you mean by "include them with ..."

Greg
A: 

glob returns an array, so you don't need to do anything else with it. example in docs shows something similar to the following:

foreach (glob("files/*.php") as $filename) {
    echo "index.php?file=".basename($filename, ".php");
}
SilentGhost
A: 

I don'know if i've understood but if you want to put all files in an array and send it to the index.php page this is the way to do it:

$files = glob("files/*.php");
$group=array();
foreach($files as $file) $group[]="file[]=$file";
$path="index.php".(count($group) ? "?".implode("&",$group) : "");
mck89
A: 

Exuse my english

Basciallty i want pages like index.php?page=signup, index.php?page=login without having to adding all pages in an array

A: 
$files = glob("files/*.php");
foreach($files as $file) $group[]="index.php?page=$file";

Now in the $group array you have the pages paths

mck89