tags:

views:

28

answers:

2

My development and production sites written in php both need to use directory iterator in order to get at some files. Directory iterator starts at the base directory of the drive i.e c:/. However, on the dev and prod servers the webroot folder is located in a different place.

Is there a way I can get directory iterator to start at the webroot. Or some similar method I can use so that I can use the same code on dev and prod without having to worry about where on the disk the application is stored.

A: 

A DirectoryIterator is instantiated with a $path. Just change that to the webroot.

DirectoryIterator::__construct()  ( string $path  )
  • path: The path of the directory to traverse.

You can store the path to the webroot in a config file per environment or determine it at runtime and save it as a constant or in a registry or other accessible place during bootstrap. For instance, if you call your direct access to you application through a FrontController that resides in index.php in the webroot, you can do:

$root = dirname(__FILENAME__);

and store that in a constant or a Registry or something.

Gordon
the point is that the $path could change so I don't want to depend on that
wheresrhys
But a bit more looking and I found that starting the path with getenv("DOCUMENT_ROOT") works
wheresrhys
A: 

Starting the path with getenv("DOCUMENT_ROOT") works

wheresrhys