why can't i use root paths in my includes?

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
cube
Forum Newbie
Posts: 2
Joined: Mon May 12, 2003 11:25 am
Location: San Jose, CA

why can't i use root paths in my includes?

Post by cube »

I'm new to PHP. I've been developing in ASP with IIS for my server environment and am trying to make a transition to the whole PHP/Apache/MySQL thing. I want to love PHP, but I've had so many frustrations that have just <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> me off that it's becoming a burden. Please help me out with this dilemma I have. Why can't I use an absolute path to call my PHP include? For instance, when I'm calling my include with a relative path like so:

<?php include('../includes/myinclude.php'); ?>

it works fine, but when I try to call it like this:

<?php include('/includes/myinclude.php'); ?>

I get an Warning: Failed opeining '/includes/myinclude.php' for includsion blah blah blah. PLEASE!!! Help me out...
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

as long as the file exists and the script's account has the proper permissions to read the file it works.
cube
Forum Newbie
Posts: 2
Joined: Mon May 12, 2003 11:25 am
Location: San Jose, CA

found it

Post by cube »

actually, i found out that I had to change the "include_path" in my php.ini file. I got it working now but thanks anyway.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

so you wanted php to prepend ../ automatically?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

The path has to be correct relative to the location of the calling script file.

Also, I never get paths to work in php unless I leave out the initial backslash ie "modules/forum/index.php" rather than "/modules/forum/index.php".
crump
Forum Newbie
Posts: 6
Joined: Tue May 13, 2003 7:04 pm

Post by crump »

I've tried everything listed in this thread and I'm still not able to include a file. I am renting server space and it appears that I can't access the php.ini file to update my include paths. I also CHMOD 'd the file calling the script.

I've tried it these ways:

Code: Select all

<?php include ("/test/tables.php"); ?>
<?php include ("test/tables.php"); ?>
<?php include ('test/tables.php'); ?>
<?php include (' //used full path to /test/tables.php'); ?>
I always get this response. (I took out the full paths)
Warning: Failed opening '/test/tables.php' for inclusion (include_path='.:/usr/local/share/pear:/usr/local/lib/php') in /test/index.php on line 5
P.S. I know it can be done because I'm running PHPNuke on the site and it is making the same calls. I'm missing something really simple, any help would be much appreciated.
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

first, a question: why does it bother you to put the ../ in front of the path?

now, a response: here's what happens, to the best of my knowledge, when you call any of the include* or require* functions. The current working directory of the PHP process is where the script is. For our example, we'll assume that it's '/var/www/php/project/php_script.php'. In a unix system, '/' is similar to the 'c:'' directory in windows; it's the base of the whole filesystem. If you write:

Code: Select all

include('/includes/script.php')
PHP looks in the directory '/includes/script.php', which (probably) doesn't exist. (at a command prompt, try 'ls /'). If it doesn't exist, PHP returns the error you've described.

Now, if you write

Code: Select all

include('../includes/script.php')
PHP sees a relative file path. That is, the '..' means 'up one directory', so PHP looks up one directory (in '/var/www/php/') for a directory named 'includes'. If it exists, it then looks for a file called 'script.php'. If one of those doesn't exist, PHP again returns the error you've described. If the both exist, PHP loads the file.

For one final example, if you write

Code: Select all

include('includes/script.php')
PHP sees another relative path, this time less specific than the first two. The first thing it does is check the current directory for a directory named "includes" with a file named "script.php" inside it. Second, it goes through all the directories in the 'include_path' variable in your php.ini, looking for an "includes" directory with a file "script.php" inside it. Again, if it can't find it, it returns an error, else it loads the file.

Hope this helps you understand what's going on.
crump
Forum Newbie
Posts: 6
Joined: Tue May 13, 2003 7:04 pm

Post by crump »

I guess there is a misunderstanding :) It wouldn't bother me to include a

Code: Select all

<?php
../include/blah.php
?>
, but that doesn't work right now for some reason. I included all the different ways that i have tried to get the file to get included and none of them worked.

I even used the absolute path from root (ie

Code: Select all

<?php
/home/name/www/html/src/whatever.file 
?>
)and it didn't work.

I know just about enough to hurt myself,lol, but i have run Linux for 4 years and have installed lots of stuff in unix type boxes but this one is bugging me :)
crump
Forum Newbie
Posts: 6
Joined: Tue May 13, 2003 7:04 pm

Post by crump »

NM, I figured it out. I'm not sure why but if I do it this way is works.

Code: Select all

<?php
include<'./includes/tables.php');
?>
Thanks for the response :)
Post Reply