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
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.

Link to comment
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.

 

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.

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.