root filepath reference in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Keys88
Forum Newbie
Posts: 2
Joined: Sun Feb 28, 2010 2:36 pm

root filepath reference in php

Post by Keys88 »

Is php’s filepath naming convention different than html’s for "/[rootfolder]"?

With picture1.jpg and menubar.php in the same “resources” folder, this line in anyfile.php calls the picture file just fine --

Code: Select all

<img src="/resources/picture1.jpg">
-- but the very next line --

Code: Select all

<?php include("/resources/menubar.php"); ?>

doesn’t call the menubar.php file (doesn’t do anything).

Either of the following variations does work, if I put menubar.php respectively up one level or down one level in folder “nav” --

Code: Select all

<?php include("../menubar.php"); ?>

Code: Select all

<?php include("nav/menubar.php"); ?>
-- which would be okay, except that I would like to use the root-folder-based references, as with the picture file example here.

Does php not use the “/” convention for specifying the root folder, as html does?

Thank you.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: root filepath reference in php

Post by requinix »

HTML doesn't know about where files are on the server. In fact, it doesn't know anything except that when it asks for "/resources/picture1.jpg" it gets back a picture.

It would be silly for a web server to allow access to everything on the computer. What it does is set up a document root: the root of where your website will be. Examples are /var/www/html and C:\inetpub\public_html. To a browser, that's where the root is.
Needless to say, that's not where the filesystem root is.


PHP doesn't know about HTML. It doesn't know that when you give it a good, legitimate path, you actually mean someplace else. You have to be specific: if you mean "where the document root is plus resources/menubar.php" you need

Code: Select all

include $_SERVER["DOCUMENT_ROOT"] . "/resources/menubar.php";
Keys88
Forum Newbie
Posts: 2
Joined: Sun Feb 28, 2010 2:36 pm

Re: root filepath reference in php

Post by Keys88 »

Thank you. I wasn’t clear. By "/[rootfolder]", yes, I meant “the root of where the website will be”, not the computer’s root directory which would indeed not make sense. The leading "/" does direct the html call to the website's root directory, and I was asking for an equivalent expression in php.

When I tried every form of the "include $SERVER, etc" line I could think of (trying to get the correct root directory name), the page wouldn't display at all, not even the few simple html lines above that line. This seems beyond the range of this beginner, so either of the two viable php lines cited above will serve.
Post Reply