PHP includes and subfolder
I've create an application which uses PHP开发者_JAVA技巧 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:
http://beginsathome.org.uk/BAAF/images/file_system.png
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).
http://www.beginsathome.org.uk/BAAF/images/screenshot.png
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
Assuming your PHP files are calling the templates, 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
.
No, $_SERVER['DOCUMENT_ROOT']
is independent from the OS or web-server, if properly set (usually it does). This method is among the best ways to make an absolute path, starting from the filesystem root.
But then your browser requests a CSS file. It requests it not from the server's filesystem, but from the virtual web-server. All you need for your cSS files, is to set its path from the server root, making it absolute (that is, "accessible from everythere"). An absolute path always start from the /
symbol.
So, according to your picture, /templates/css/baaf.css
would be OK.
Try using ../
in your path to move up out of the current directory first.
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
精彩评论