0

I wanted to set up a config directory where all the files automatically inside this directory will be loaded with every page.

My goal is to put a bunch of functions and defines in there so for example

config/index.php
config/defines.php 

and so and have these files all automatically load every time. Anyone know how i can do that

j08691
  • 204,283
  • 31
  • 260
  • 272
Yeak
  • 2,470
  • 9
  • 45
  • 71
  • With [`glob`](http://php.net/glob), [`foreach`](http://php.net/foreach) and [`include`](http://php.net/include). – mario Jan 13 '13 at 21:33
  • I haven't quite figured out what [Midgard and PHPCR *does*](http://midgard-project.org/), but maybe it's relevant here? (Feel free anyone to explain otherwise what these are about if this is off-base.) – Jared Farrish Jan 13 '13 at 21:36
  • By the way, how many config files do you have in there where you don't just want to list them out in the source...? – Jared Farrish Jan 13 '13 at 21:43

2 Answers2

1
$files = glob('config/*.php');
foreach ($files as $file) {
    include $file;
}
Jeroen
  • 13,056
  • 4
  • 42
  • 63
  • where would i put this in though? Im not too familiar with server set ups such as apache so where would i put this function in? – Yeak Jan 13 '13 at 21:36
  • Just put it in the top of your `index.php`, or whatever file you're loading first. – Jeroen Jan 13 '13 at 21:37
  • This still requires me to be dependant on a specific file loading though. Say i have a autoload file that i put this function in. How can i get that file to be loaded or included in every single file without manually including it? – Yeak Jan 13 '13 at 23:07
1

Check out the glob() function for a simple way of iterating through files in a directory.

Obviously, you will need to make sure that nothing untrusted can add files to that directory.

IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • where would i put this function in though? What file or directory would i put this in – Yeak Jan 13 '13 at 21:36
  • @Yevo Wherever you would put the `include` line if you were just including one file. If you want to run it at the top of lots of different files, you could put it in a file of its own and add something like `include 'include_config_files.php';` or `include 'startup.php';` – IMSoP Jan 13 '13 at 21:43