user878729
user878729

Reputation: 93

require_once path can't locate file?

Here is my folder structure as example:

source files/
    controller/
        ctrl_showData.php
    model/
        Database.class.php

Suppose it's now in controller directory, and in the ctrl_showData.php, I want to require the Database.class.php, so I try:

require_once(dirname(__FILE__) .'/../model/Database.class.php');

But when I debug, the program stalls when it execute the statement above, which I guess it can't locate the file. I already read similar questions and tried their solutions, but none work. Any one could help? Thanks! Here is the echo result:

/controller/../model/Database.class.php

Apparently, it doesn't go the parent directory, which is the source files/, then go to the model/ , but don't know why?

Thanks for the error message suggestions, the error message is:

Warning: require_once(config/config.php): failed to open stream: No such file or directory in /model/Database.class.php

Because I also require the the config.php in the database file, which the path is not correct. I didn't think about that, thank you for all your help!

Upvotes: 1

Views: 9884

Answers (5)

Mathieu Dumoulin
Mathieu Dumoulin

Reputation: 12244

What is the error in your error log? Do you get something that says the file can't be found? Do you get an error at all?

Use:

error_reporting E_ALL
ini_set('display_errors', 1);

And then see what is the error. If your error is really about including a missing file, check that the path is right, also check that your include path is right. You could by error have removed the "." in the include_path directive or your CMS could have done so if it wants to restrict you to using their autoloader.

Another idea is to EXIT('BLA') in your file and call your page again to see if the BLA appears, if it does, it means your file is included but there is bug in it and thats why you stall.

Upvotes: 0

user904550
user904550

Reputation:

Try this:

require_once(realpath(dirname(__FILE__) . '/..') . '/model/Database.class.php');

Upvotes: 6

Richard Niemand
Richard Niemand

Reputation: 161

You could try using:

include('./model/Database.class.php')

Upvotes: 1

Joshua Kaiser
Joshua Kaiser

Reputation: 1479

Try wrapping that call in realpath() http://us.php.net/manual/en/function.realpath.php

If your path isn't resolving correctly it will return null, and it cleans up the relative path mess you have in the middle.

Upvotes: 0

Rene Pot
Rene Pot

Reputation: 24815

You can use a relative path, so using this (below) should just be working:

require_once('../model/Database.class.php');

Upvotes: 0

Related Questions