note 104265 modified in function.require-once by danbrown

From: Date: Thu, 09 Jun 2011 14:21:42 +0000
Subject: note 104265 modified in function.require-once by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-180386@lists.php.net to get a copy of this message
If your code is running on multiple servers with different environments (locations from where your scripts run) the following idea may be useful to you: a. Do not give absolute path to include files on your server. b. Dynamically calculate the full path (absolute path) Hints: Use a combination of dirname(__FILE__) and subsequent calls to itself until you reach to the home of your '/index.php'. Then, attach this variable (that contains the path) to your included files. One of my typical example is: <?php define('__ROOT__', dirname(dirname(__FILE__))); require_once(__ROOT__.'/config.php); ?> instead of: <?php require_once('/var/www/public_html/config.php); ?> After this, if you copy paste your codes to another servers, it will still run, without requiring any further re-configurations. [EDIT BY danbrown AT php DOT net: Contains a typofix (missing ')') provided by 'JoeB' on 09-JUN-2011.] --was-- If your code is running on multiple servers with different environments (locations from where your scripts run) the following idea may be useful to you: a. Do not give absolute path to include files on your server. b. Dynamically calculate the full path (absolute path) Hints: Use a combination of dirname(__FILE__) and subsequent calls to itself until you reach to the home of your '/index.php'. Then, attach this variable (that contains the path) to your included files. One of my typical example is: define('__ROOT__', dirname(dirname(__FILE__)); require_once(__ROOT__.'/config.php); instead of: require_once('/var/www/public_html/config.php); After this, if you copy paste your codes to another servers, it will still run, without requiring any further re-configurations. http://php.net/manual/en/function.require-once.php

« previous php.notes (#180386) next »