I'm brand-spanking new to PHP, so please be gentle.
I'm trying to create a dynamically-populated page navigation for a frequently-updated site. I thought I'd use something like GLOB_ONLYDIR
or is_dir
(all the pages I'm looking for will be in their own directories) to generate an array that the navigation can build itself from, but from what I've read frequent calls to the server like that (scanning up to fifty directories, creating the array on every page visited, and who knows how many site visitors) can be taxing on the server.
Is this true? I'd like to avoid using an XML for the directory, but does calling php that often create a memory drain (on what is admittedly already a memory-heavy site - lots of motion graphics, transparencies, javascript animation, etc), or should it be no more a drain than your usual php page generation?
Thanks, ~gyz
lots of motion graphics, transparencies, javascript animation, etc
The above mentioned things won't create a drain on the server but a drain on the users browser and computer. And there are many sites which have way more calling of php than what you are expecting here. It won't destroy your server so don't worry.
But i would recommend that you use something like json or xml for it.
Yes, you should avoid scanning your filesystem. In fact, you should only refresh your dir list when a change occur. If you want to avoid using of XML, you can use different method, e.g. serailaized nested JSON arrays saved to a file.
In terms of memory consumption, thta stuff you were talking about (javascript, transparencies etc. ) is about browser consumption, not the server
Regards Jean
One thing you could do is cache the nav (either create XML or the HTML content itself) and have a script update it once every 24 hours or something. Have your main page include the cached page in the header.
As far as memory usage, you said yourself that it's a memory-heavy site, so yes I think running code that checks 50+ directories every time a page on your site loads would be an issue.
Doing this may very well be the sanest way to build up the navigation structure. Having to do it with every single request, probably not so much.
You should build some sort of caching mechanism to handle this. Like this:
if (!file_exists($cache_file || filemtime($cache_file) + $cache_lifetime > time()){
// build your name and save it to the cache_file
}
// output your nav.
$fp = fopen($cache_file,"r");
fpassthru($fp);
$fclose;
When you are adding files and you want to invalidate the cache just delete it and it will automatically be regenerated at the next request. Also this will expire and force recreation $cache_lifetime seconds after it was created.