Help Me Slap my Forehead

SkyHog

Touchdown! Greaser!
Joined
Feb 23, 2005
Messages
18,431
Location
Castle Rock, CO
Display Name

Display name:
Everything Offends Me
I'm pulling a major stupid on a minor issue. Please someone help me.

There's a folder structure...lets say it looks like this:

Folder1
-subfolder1


etc.

Inside subfolder1 is a file, we'll call it "index.php"

I want to include a file that is in Folder1. Tell me why

Code:
include "../topstuff.php";

Works, but
Code:
include "/topstuff.php";

Does not, when Folder1 is the top level? I know I'm being retarded....
 
All the way back to DOS days, .. says "parent folder".
 
Nick, the other possible issue is confusing DocumentRoot with the physical file structure. When you're doing an include, it's generally going to be looking at the physical file structure.
So let's say you've got a file like
/etc/apache/htdocs/index.php
and your website is www.thewrightstuff.net with a DocumentRoot of /etc/apache/htdocs. You would access the file in a browser as www.thewrightstuff.net/index.php. If it had an include that attempted to bring in a file /etc/apache/htdocs/myinclude.php you could write it as any of:
Code:
include "./myinclude.php"
include "myinclude.php"
include "/etc/apache/htdocs/myinclude.php"
And if your includes were actually something like /etc/apache/inc/header.inc then you could refer to it as
Code:
include "../inc/header.inc"
Note that in this case the user should not be able to do this in their browser to see the contents of the file:
www.thewrightstuff.net/../inc/header.inc
If they could, it would mean that they could look at any file in your file system.
 
I'm pulling a major stupid on a minor issue. Please someone help me.

There's a folder structure...lets say it looks like this:

Folder1
-subfolder1


etc.

Inside subfolder1 is a file, we'll call it "index.php"

I want to include a file that is in Folder1. Tell me why

Code:
include "../topstuff.php";
Works, but
Code:
include "/topstuff.php";
Does not, when Folder1 is the top level? I know I'm being retarded....

Ahh Nick.. The joy of paths in PHP. Your second include does not work because that code is executed on the SERVER side. It is looking for the file in the / (root) of the filesystem.

DO NOT DO NOT DO NOT DO NOT DO NOT DO NOT DO NOT DO NOT DO NOT use relative paths in your includes. Did I make that clear?

The best thing is to set a constant that has the full system path to the root of your code and then build all your includes based on that.

For example--
Code:
define("FULLPATH", $_SERVER['DOCUMENT_ROOT'];
include(FULLPATH.'/topstuff.php');
If your server document root is set to /var/www/virtual/nick.com/html this would work out to /var/www/virtual/nick.com/html/topstuff.php. The cool thing about this is that it'll work when you move the code around between servers.

So once again..always do includes based on full absolute paths and build your full absolute path with a constant in a main include and then use that constant in all your includes.

$_SERVER['DOCUMENT_ROOT'] will always contain the path to the document root for that virtual host.

If you're writing a lot of code these days--you might want to consider using a framework. It has a lot of this already nailed out for you. http://www.kohanaphp.com
 
Last edited:
Darn it... by the title I thought I was going to have some fun but then it's just computer gibberish.
 
Ahh Nick.. The joy of paths in PHP. Your second include does not work because that code is executed on the SERVER side. It is looking for the file in the / (root) of the filesystem.

DO NOT DO NOT DO NOT DO NOT DO NOT DO NOT DO NOT DO NOT DO NOT use relative paths in your includes. Did I make that clear?

The best thing is to set a constant that has the full system path to the root of your code and then build all your includes based on that.

For example--
Code:
define("FULLPATH", $_SERVER['DOCUMENT_ROOT'];
include(FULLPATH.'/topstuff.php');
If your server document root is set to /var/www/virtual/nick.com/html this would work out to /var/www/virtual/nick.com/html/topstuff.php. The cool thing about this is that it'll work when you move the code around between servers.

So once again..always do includes based on full absolute paths and build your full absolute path with a constant in a main include and then use that constant in all your includes.

$_SERVER['DOCUMENT_ROOT'] will always contain the path to the document root for that virtual host.

If you're writing a lot of code these days--you might want to consider using a framework. It has a lot of this already nailed out for you. http://www.kohanaphp.com

You are the man, Jesse, thanks for the help.

I am avoiding using frameworks just because the few things I don't know about php (like this, for example), I learn by doing.

Plus, I really enjoy programming. Frameworks seem to take the fun out of it - like using autopilot instead of rightie. Sure, its easier, and probably does the job better, but man, hand flying wit rightie or leftie sure is more fun.
 
Back
Top