Jump to content

Directory structure issue


khaos337
 Share

Recommended Posts

I currently have a website that was thrown together w/o planning and I am now trying to begin organizing it a little better. The issue I'm having is that I want to move some of the pages into subdirectories, but other pages (such as the main page) will still remain in the root directory. I have a directory called includes that holds the header file, footer file, and navigation files which are included in all pages within the site. My problem is that when I am on a page within the root directory, or a page within the sub directory, the path's need to be different for the links and image files. For instance, if a page in the root directory is displayed to link to another page I can just link to "example.php", but if i'm on a page within a subdirectory the link needs to be "../example.php". Only using one file to hold all the link information, is there a simple way to do this?

Edited by khaos337
Link to comment
Share on other sites

A common way is to have a single include file which gets included by every page, where you can define variables like the path to the root of the site, include other common files, change configuration settings, etc. Then all of your include statements in all of the other files can use the root path variable in the include path. You can also change the default include path for PHP, but that's another change you need to make on every page, so might as well just use a single include file to define global settings and things like that.

Link to comment
Share on other sites

I understand the theory behind it, but I'm sure I'm missing something obvious in the implementation. Let me try to be a little more specific. I will use an image as my example, but obviously the same applies to linking to other pages.Lets say we're working with the following 4 directory's1. The root directory of a site, www.example.com containing only an index.php file2. A directory called example also containing an index.php file3. A directory called include containing definitions.php4. A directory called images containing header.pngboth index.php files are identical (accept of course the path to the definitions.php which will be "../definitions.php" in the index.php contained within a sub-directory):

<html><head></head><body><? include('definitions.php'); ?><img src="<? echo IMAGE_DIR ?>header.png"/></body></html>

definitions.php looks like this:

<?define('IMAGE_DIR', '/images/');?>

Now when you load www.example.com, it's going to look in the right directory - www.example.com/images/But when you load www.example.com/example, it will look for the image in - www.example.com/example/images - of which there is no such directory.Short of defining my images directory as 'http://www.example.com/images' i'm not sure what to do. That may be the solution, but is it a good one? Is it the only one?

Link to comment
Share on other sites

/images/it will always look into your web root directory. (wacth the forward slash). means it will always look into http://www.example.com/imagesif you use only images/ then it will look into current directory

Edited by birbal
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...