views:

65

answers:

2

Hello,

I want to make a little script but I'm rather n00b in php so please help me if you can :)

What I want the script to do is:

  1. I have "index.php".
  2. I want "index.php" to read the sub-directories from a known folder, let's say "templates/"
  3. Each sub-directory will contain a file called "content.html".
  4. The index will then load the "content.html" with the require() function for each of the existing sub-directories.

Thank you very much for your help!

+3  A: 

The RecursiveDirectoryIterator runs through the designated directory and all subdirectories and returns all filesystem items as SplFileInfo objects:

$iterator = new RecursiveIteratorIterator(
                new RecursiveDirectoryIterator($yourFilePath));

foreach($iterator as $file) {
    if($file->getFilename() === 'content.html') {
        $file->openFile()->fpassthru();
        // or 
        include $file->getRealpath();
    }
}

Use include when you want to run the .html file through the PHP parser, e.g. when it contains PHP code that needs to be evaluated. If not, use fpassthru, because that should be a fraction faster. Can also use readfile($file->getRealpath()).

Gordon
A: 

Hello Max,

what do you want to achieve?

Do you want the content of the different files in your index.php or do want to generate a link to these files?

Because if you want to aggregate the content, this approach is not enough because you get a html file with invalid syntax, because you simply concatenate the content of the html files.

You will need some additional parsing of the content to avoid this.

Cheers,

Dawit

da8
Each sub-directory will be a product, so I want index to check every time it loads if there is something new in that folder.the "content.html" file or "content.php" file in each sub-directory will contain some html code for text/images (the description of that product). All the products will be displayed in a column on the first page (index.php)I know there are better ways and shop scripts but this is my way :)
Adrian M.
I was just wondering if content.html is a fully fledged html file but if it contains only fragments of html or php than concatenating will produce the result you want.
da8
only fragment (a table or a div)
Adrian M.