tags:

views:

287

answers:

4

Hi,

I've create an application which uses PHP includes which works great if you don't mind having the PHP include files in the same place as the other files. But I would like to split my code up as neatly as possible. Please see the below file structure:

As you can, I would like all ?.php files to be in the document root and for all php-related include files (eg. database connection details) to be in the 'inc' folder.

For the html and corresponding css, images etc, I created a 'templates' folder to hold all the details with a sub-folder called 'includes' which will hold the html includes such as header, footer etc.

Unfortunately, even though my images and css are in the same place as my html templates, the css and images don't seem to show when viewing the site (although the text part of the header/footer do).

Ive been trying to do some research and understand that using something like


$path = $_SERVER['DOCUMENT_ROOT'];
   $path .= "/common/header.php";
   include_once($path);

might be a way forward, but i am new to PHP and wasn't sure where to put it, or which route to specify. I also understand that this method can vary depending on whether it is on an apache, linux or windows server. ideally i want the method to work on all servers. I am currently developing on a local apache server on a mac.

Thanks very much for your help Theresa

A: 

No, $_SERVER['DOCUMENT_ROOT'] is independent from OS or web-server, if properly set (usually it does). this method is among the best ways to make absolute path, starting from the for the filesystem root

But then your browser request CSS file, it request not from the server's filesystem, but from the virtual web-server. all you need for your cSS files, is to set it's path from the server root, making it absolute (i.e. "accessible from everythere"). Absolute path always start from the / symbol.

So, according to your picture, /templates/css/baaf.css would be ok

Col. Shrapnel
I, i've tried /templates/css/baaf.css but this doesn't work. The strange thing is, in my error.html.php file I have used <?php include 'includes/header.inc.html.php'; ?> and the include files obviously are called correctly because they appear (unstyled). Therefore I have used the same method for my css 'css/baaf.css' but it doesn't work.
Theresa
you include a _file_ but css is not a file, but URI. The file being called by your script, but css called by browser. You have to check the real paths of your system. If you call /templates/css/baaf.css directly in the browser address bar, what error do you see - 404 or something else? is your templates folder at the webserver root?
Col. Shrapnel
Hi. I'm using MAMP so its MAMP/htdocs/b@h/templates (b@h is my root for this application). I've looked at http://localhost:8888/b@h/templates/css/baaf.css and it shows me the css. If i access my template directly 'http://localhost:8888/b@h/templates/error.html.php' then the CSS and images are correctly displayed...
Theresa
bingo! this /b@h/ is the thing you need./b@h/templates/css/baaf.css is the absolute path
Col. Shrapnel
Genius! Yep done that. But even though htdocs in my apache document root, for my application's point of view i'd like within b@h to be my applications root - so how do i go that (thanks again!)
Theresa
htdocs is not in document root, it **is** document root itself
Col. Shrapnel
+1  A: 

I assume your php files are calling the templates? If so, the actual document root for that file is includes, NOT the html files root.

Therefore, you need to traverse to the css directory: ../templates/css/FILE.css

Cryophallion
Hi, thanks... but i've tried and nothing appears to have changed :(
Theresa
do me a favor: echo getcwd();, and update your question with that. Also, are you using any .htaccess files or virtual roots? If you don't know, you probably aren't.
Cryophallion
echo getcwd(); = /Applications/MAMP/htdocs/b@h/templates. No, i'm not using .htaccess files atm - i've only every used them on web servers and this is my first time developing locally
Theresa
Well, welcome to local dev on php. So, since we know you are in the templates folder, css/FILE.css should be what you are looking for.
Cryophallion
Actually, wait a second. It looks like you are coding on a mac. What are the permissions on those directories? It may also be that the server does not have permissions to view the files (but if you ust opened the file, it would work, as you are manually loading the file, instead of localhost).
Cryophallion
I changed my CSS path to /b@h/templates/css/baaf.css which works... but obviously I want the document root to start within b@h so how can I do that? Do I use a .htaccess or do I assign my document_root to a variable and call that variable?
Theresa
It depends. You can do one of 3 things: use a symlink to the css folder (symfony does it like this), use and htaccess, or define a document root in a settings or constants file (I also use this for db access variables) by using a constant (http://us3.php.net/manual/en/language.constants.php)
Cryophallion
A: 

Try using ../ in your path to move up out of the current directory first.

Gary Willoughby
Hi, thats what i tried (as this works with html) but no, it doesn't change.
Theresa
A: 

If I may suggest that you change your dir structure around to something along the lines of:

  • docroot
    • css
    • images
    • javascript

for your doc root, then just make sure the php_include_path is set to:

  • includes
    • templates
    • _notes
    • etc. etc.

should make things a little easier to organize

sobedai