imgrooot Posted May 28, 2017 Share Posted May 28, 2017 Say I have directory setup like this. core/ includes/ snippets/ template.php js/ index.php Say in the index.php file, I have a file path like this " include 'snippets/template.php'; ". It works. Or say If I am in template.php and I have a file path like this " include '../index.php'; ". It works as well. I am wondering if there is a more global method to this so I don't have to worry about when to use "../" or not. It becomes hectic if there are a lot of includes and/or links. Quote Link to comment Share on other sites More sharing options...
requinix Posted May 28, 2017 Share Posted May 28, 2017 The DOCUMENT_ROOT is the path to the root of your website. include $_SERVER["DOCUMENT_ROOT"] . "/snippets/template.php"; Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted May 28, 2017 Share Posted May 28, 2017 Putting your entire application into the document root is not a great idea. It means that every internal script can be accessed directly, which may lead to unexpected side effects. And if your webserver ever fails to handle .php files correctly, maybe just for a few minutes, then the application suddenly becomes downloadable, including all your configuration data (passwords, keys etc.). The document root is for public resources like your index.php, images, CSS, JavaScript etc. But all internal files (classes, functions, templates, configuration files, ...) belongs into a separate directory outside of the document root. The approach I take is to have the webserver set an environment variable with the path to the internal application directory. Then the index.php can load an initialization script which sets up the autoloaders and/or helper functions for script inclusion. <?php // get environment variable with the path of the internal web application directory $app_base_dir = getenv('APP_BASE_DIR'); if (!$app_base_dir) { throw new RuntimeException('Environment variable APP_BASE_DIR is not set.'); } // include initialization script which sets up autoloaders, defines helper functions for script inclusion etc. require_once $app_base_dir.'/bootstrap.php'; // now you can just start creating classes $router = new \YourCompany\Core\Router(); // or if you're into oldschool PHP, include scripts include_template('navigation.php'); // the function will take care of resolving the path, e. g. /path/to/app/templates/navigation.php include_functions('html'); But manual script inclusion should be getting rare these days, anyway. Thanks to autoloaders and template engines, PHP can do that all by itself once you've provided a few base directories. 1 Quote Link to comment Share on other sites More sharing options...
kicken Posted May 28, 2017 Share Posted May 28, 2017 If you can't or don't want to mess with your server configuration, an alternative is to infer the application root based on the location of your index.php file using the __DIR__ magic constant. $app_base_dir = realpath(__DIR__ . '/..'); Assuming your index.php is in your document_root, then $app_base_dir would point to it's parent directory. The call to realpath() isn't really necessary but I like to do it. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted May 28, 2017 Share Posted May 28, 2017 You shouldn't “assume” anything. Directory structures are wildly different on different servers, and an application which has hard-coded “assumptions” buried in its code is a pain in the ass. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.