Jump to content

require_once problems


Hate

Recommended Posts

I'm writing a rather large script and it appears that I'm running into some require_once problems. So far I only have a few files wrote for my script..

 

./includes/objects/database.class.php

require_once("../../config.php");

 

./test.php

require_once("./config.php");
require_once("./includes/objects/database.class.php");

 

When I try running the test.php I get the following error:

Warning: require_once(../../config.inc.php) [function.require-once]: failed to open stream: No such file or directory in /includes/objects/database.class.php on line 27

Fatal error: require_once() [function.require]: Failed opening required '../../config.inc.php' (include_path='.:/opt/lampp/lib/php') in /includes/objects/database.class.php on line 27

 

However, when I run the database.class.php by itself it doesn't have any errors at all and it requires (sounds weird.. includes I guess?) the file just fine. I only get the above error from running test.php. I'm running on a local environment using XAMPP so there might be some php configuration option that I'm not aware of. I've tried doing some reading and I'm honestly not sure what could be the problem.

 

Help?  :'(

Link to comment
https://forums.phpfreaks.com/topic/213247-require_once-problems/
Share on other sites

Avoid daisy chaining includes. If you do, you should use absolute paths. Not relative ones.

 

The reason it doesn't work is the same reason that if you put

require_once("../../config.php");

into test.php it wouldn't work. It's looking two directories down from test.php. Not database.class.php. It doesn't matter that database.class.php is in a subdirectory.

 

Hope that makes sense.

Avoid daisy chaining includes. If you do, you should use absolute paths. Not relative ones.

 

The reason it doesn't work is the same reason that if you put

require_once("../../config.php");

into test.php it wouldn't work. It's looking two directories down from test.php. Not database.class.php. It doesn't matter that database.class.php is in a subdirectory.

 

Hope that makes sense.

 

Basically, what you're saying is that test.php takes all of the require_once from all includes and loads them from the test.php directory level?

 

If that's the case then how could I use absolute paths in my classes? I doubt the file that loads the class will always have the configuration loaded. It would be nice to have it in both without errors. Then if nothing else includes the configuration the database class will always have it regardless.

If that's the case then how could I use absolute paths in my classes?

This is why many PHP applications will have a path variable in their config.

 

You can do something like $path = '/var/www/html/"; in your config.php, then make your includes/requires like this:

require_once($path . 'includes/objects/database.class.php');

 

Or you can stop using include functions in your included files (as I said, daisy chaining your includes).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.