path issues<

I'm working on a web app that has php scripts/classes in a directory, "src" outside of the webroot. src has a config file with various info including db access info. src also has a php class used to connect to the database.

I have also created a directory, "scripts" that contains a php script, feature.php, that will be run as a cron job. The issue is that feature.php will update the database and needs to use the database class in "src"

Since they are in 2 different directories outside of the webroot, I can't use .htaccess to give an include path.

I tried putting in the feature.php the following:

require_once("/src/DatabaseUtility.php");

but when I run the script it can't find the DatabaseUtility.php.

Could someone give me some direction as to how to set this up correctly?

Thanks!You should try using the full document root to the file, like this...


<?php

require_once("{$_SERVER['DOCUMENT_ROOT']}/directory/file.php");

?>


So for yours, you have a directory with the src directory in it. Try this...


<?php

require_once("{$_SERVER['DOCUMENT_ROOT']}/scripts/src/DatabaseUtility.php");

?>When I put in the full document root like below I get the warnings/fatal error below:

code in the set_feature.php script
----------------------------------------
require_once("{$_SERVER['DOCUMENT_ROOT']}/src/php/com/domainname/includes/config.php");
require_once("{$_SERVER['DOCUMENT_ROOT']}/src/php/com/domainname/database/DatabaseUtility.php");
---------------------------------------

errors when running it command line with 'php -f set_feature.php'
---------------------------------------------------


Warning: main(/src/php/com/domainname/includes/config.php): failed to open stream: No such file or directory in /home/[username]/scripts/set_feature.php on line 3

Fatal error: main(): Failed opening required '/src/php/com/domainname/includes/config.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/[username]/scripts/set_feature.php on line 3


I also tried putting in the paths:

require_once("home/[username]/src/php/com/domainname/includes/config.php");
require_once("home/[username]/src/php/com/domainname/database/DatabaseUtility.php");

but got the following errors:

----------------------------------
Warning: main(home/[username]/src/php/com/domainname/includes/config.php): failed to open stream: No such file or directory in /home/[username]/scripts/set_feature.php on line 5

Fatal error: main(): Failed opening required 'home/[username]/src/php/com/domainname/includes/config.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/[username]/scripts/set_feature.php on line 5

p.s. I changed the file structure a little bit, as reflected in the error messages aboveSo your path is /home/[username]/oh yeah, I forgot to put in the beginning slash...

but I would rather not hard code that in anyway since this is a development project that will move over to a "live" server later.

one problem...when I try to use $_SERVER['DOCUMENT_ROOT'] it gives me a NULL value. do you know why that would be?

if I just put in the test line in the script...

echo "the doc root is " . $_SERVER['DOCUMENT_ROOT'];

it doesn't print the $_SERVER['DOCUMENT_ROOT'] value
 
Back
Top