Re: Path question
| From: | Paul M Foster | Date: | Tue, 29 Mar 2011 03:50:37 +0000 |
| Subject: | Re: Path question | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-312121@lists.php.net to get a copy of this message | ||
On Mon, Mar 28, 2011 at 09:18:39PM -0400, Jack wrote:
> Hello All,
>
>
>
> Is there a smarter way to do includes by setting up a path or something
> where I don't have to include /home/domain.com/includes/include_file.php
>
> Apparently my path is as shown above, but I would prefer to just put in
> /includes/include_file.php
>
As a first action, in whatever file you first include or call, do the
following:
$site_path = realpath(dirname(__FILE__)) . DIRECTORY_SEPARATOR;
define ('SITE_PATH', $site_path);
Next, create a function like:
function include_path($filename)
{
$str = SITE_PATH . 'include' . DIRECTORY_SEPARATOR . $filename;
return $str;
}
Now, when you want to include a file, do
include(include_path('something.php'));
In my projects, I have a variety of directories, like libraries, views,
models, etc. So the function I use has a second parameter:
include(fullpath('model', 'pizza.php'));
include(fullpath('include', 'gesundheit.php'));
Paul
--
Paul M. Foster
http://noferblatz.com
http://quillandmouse.com