I have a site with an index.php. The index.php has a number of include files like
<?php include_once('scripts/php/index_mainImageJoin.php');?>
I've created a new folder off the root called 'extrapages' and I'm going to have pages in there that have information relating to the site. I've added the include_once files like above and change the path and these hook up fine.
The problem I'm find is paths within the include files fail. eg: if an image or another include file is within the include it fails when run from the 'extrapages' folder. Pathing issue.
Is there a good way to deal with this? Can I change/set the path to the site root (www) for pages under 'extrapages' to one folder down by chance?
I could move these pages onto the root and they would run fine but I really don't want all the clutter on the root of the site.
Any ideas & thx
The key to any path problem is called absolute path
while creating hyperlinks for your site (including image sources), always start it from
/
followed by full correct path. And it never fail you.same for the filesystem calls: always use absolute path. Your server usually provides you with very handy variable called
$_SERVER['DOCUMENT_ROOT']
contains the point where filesystem meet web-server, pointing to your web root directory.So, when called from anywhere in your site,
will point always to the same location
Just add your include path once (somewhere at the beginning or in a config file) using
set_include_path()
, see the manual. Use an absolute path (not relative; can utilizedirname(__FILE__)
) and it should work all the time.you should use
dirname
and the__FILE__
by using this both constant you should be able to include file relative to the current file instead of the php script called by the web server.for example
dirname
: would return the directory part of a path__FILE__
: is a magic constant, it's replaced by the path of the current file.The only problem with doing such thing you lock the structure of your project but most of the times it's acceptable.
If you're on PHP 5.3.0 or newer, you can (instead of what RageZ) suggested, use just
__DIR__
(a newly defined magic constant).example:
Now, this doesn't help when you want to avoid
../
and mapping out your includes. There's a better way to do it, though - in all front-end files (which should be the ONLY user-accessible PHP files) you define a constant which provides the root path of your script (and not of the current file).For example:
index.php
Now let's say we want to include a file in our includes directory.
Let's do it in
includes/myinclude.php
, and include the fileincludes/myotherinclude.php
includes/myinclude.php
Keep in mind that the include paths should be directly relative to the root directory of the project itself, not to just one of the front-end files. If you have a front-end file in a subdirectory, you need to back out to the project root itself when defining the constant.
example:
subdirectory/index.php
index.php
All we do here is add a dirname() call, which takes off a directory in the path. See: http://us.php.net/manual/en/function.dirname.php