include path problems

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
waylon999
Forum Commoner
Posts: 26
Joined: Mon Mar 23, 2009 5:29 pm

include path problems

Post by waylon999 »

I am running php 5.2.5 on Ubuntu 8.10, and I am having trouble understanding how the paths work.
I have a couple of includes in my script:

Code: Select all

require_once('role_functions.php');
require_once('SOAP/Client.php');
 
For the sake of this post I will say that my script is in the /usr/bin folder. When I call my php script from the /usr/bin folder, everything works fine. When I call the script from the /usr folder, the SOAP/Client.php include doesn't seem to work correctly ('The error message is pretty code specific, so I left it out). If I change the require_once to:

Code: Select all

require_once('role_functions.php');
require_once('/usr/bin/SOAP/Client.php');
 
Then I get a different error (again it's related to the code and probably wouldn't help much).

Can anyone help explain to me how php and linux behave when calling a script from a location different from where the script is?

Thanks!
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: include path problems

Post by JAB Creations »

Let's say the file location you're at is...

/web/public_html/

Then this...
SOAP/Client.php

..translates in to PHP thinking the file is located at...
/web/public_html/SOAP/Client.php

However if your file is located at...
/web/SOAP/Client.php

...then you need to request the file from a different directory then what you're currently in. You need to use the double dots to do something like that. In example...
../SOAP/Client.php
waylon999
Forum Commoner
Posts: 26
Joined: Mon Mar 23, 2009 5:29 pm

Re: include path problems

Post by waylon999 »

Ok, so the path that php is using should be relative to the scripts location and not the location of where it is being called from? The issue is that I need to run a script via cron, but there always seem to be errors happening when it's run from cron vs. it working fine when I call it from the scripts directory.

The strange part is that I'm not getting errors saying that the include files cannot be found, but there are always errors with the include files when the script is called from a different location.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: include path problems

Post by JAB Creations »

If the issue is with cron jobs then why didn't you mention that in both the original post and the thread title?! :roll:

I have very minimal knowledge about cron jobs. You'll need to edit your first post and amend the title to include "with cron jobs" so someone with more cron job experience can aid you.

That being said you should take advantage of PHP's $_SERVER variable to give you more information about the status of the page you're working with.

Code: Select all

<?php
echo '<div>'.$_SERVER['PHP_SELF'].'</div>';
echo '<div>'.$_SERVER['argv'].'</div>';
echo '<div>'.$_SERVER['argc'].'</div>';
echo '<div>'.$_SERVER['GATEWAY_INTERFACE'].'</div>';
echo '<div>'.$_SERVER['SERVER_ADDR'].'</div>';
echo '<div>'.$_SERVER['SERVER_NAME'].'</div>';
echo '<div>'.$_SERVER['SERVER_SOFTWARE'].'</div>';
echo '<div>'.$_SERVER['SERVER_PROTOCOL'].'</div>';
echo '<div>'.$_SERVER['REQUEST_METHOD'].'</div>';
echo '<div>'.$_SERVER['REQUEST_TIME'].'</div>';
echo '<div>'.$_SERVER['QUERY_STRING'].'</div>';
echo '<div>'.$_SERVER['DOCUMENT_ROOT'].'</div>';
echo '<div>'.$_SERVER['HTTP_ACCEPT'].'</div>';
echo '<div>'.$_SERVER['HTTP_ACCEPT_CHARSET'].'</div>';
echo '<div>'.$_SERVER['HTTP_ACCEPT_ENCODING'].'</div>';
echo '<div>'.$_SERVER['HTTP_ACCEPT_LANGUAGE'].'</div>';
echo '<div>'.$_SERVER['HTTP_CONNECTION'].'</div>';
echo '<div>'.$_SERVER['HTTP_HOST'].'</div>';
echo '<div>'.$_SERVER['HTTP_REFERER'].'</div>';
echo '<div>'.$_SERVER['HTTP_USER_AGENT'].'</div>';
echo '<div>'.$_SERVER['HTTPS'].'</div>';
echo '<div>'.$_SERVER['REMOTE_ADDR'].'</div>';
echo '<div>'.$_SERVER['REMOTE_HOST'].'</div>';
echo '<div>'.$_SERVER['REMOTE_PORT'].'</div>';
echo '<div>'.$_SERVER['SCRIPT_FILENAME'].'</div>';
echo '<div>'.$_SERVER['SERVER_ADMIN'].'</div>';
echo '<div>'.$_SERVER['SERVER_PORT'].'</div>';
echo '<div>'.$_SERVER['SERVER_SIGNATURE'].'</div>';
echo '<div>'.$_SERVER['PATH_TRANSLATED'].'</div>';
echo '<div>'.$_SERVER['SCRIPT_NAME'].'</div>';
echo '<div>'.$_SERVER['REQUEST_URI'].'</div>';
echo '<div>'.$_SERVER['PHP_AUTH_DIGEST'].'</div>';
echo '<div>'.$_SERVER['PHP_AUTH_USER'].'</div>';
echo '<div>'.$_SERVER['PHP_AUTH_PW'].'</div>';
echo '<div>'.$_SERVER['AUTH_TYPE'].'</div>';
?>
waylon999
Forum Commoner
Posts: 26
Joined: Mon Mar 23, 2009 5:29 pm

Re: include path problems

Post by waylon999 »

The reason that I didn't mention cron jobs was because the issue is not limited to being run from the cron. The issue is that when the script is called from a directory other than the one it is in. Cron just happens to call the script from a directory other than the one the scirpt is in, but it behaves the same as if I called the script myself from a different directory.

I didn't want people to get hung up on it being a cron issue, when it's not really a cron issue..
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: include path problems

Post by JAB Creations »

Did you get the issue resolved though?
JasonDFR
Forum Commoner
Posts: 40
Joined: Wed Jan 07, 2009 3:51 am

Re: include path problems

Post by JasonDFR »

Post the errors you are getting.
waylon999
Forum Commoner
Posts: 26
Joined: Mon Mar 23, 2009 5:29 pm

Re: include path problems

Post by waylon999 »

I found out what the issue was, I had to set the include path to include the script location, at the beginning of my script.

Code: Select all

ini_set('include_path','/path/to/include')
I assumed that the location of the script would be automatically added to the include path, but I guess not. Maybe there's a setting in the ini file somewhere.......

Thanks for your help!
Post Reply