I have a question about require_once in php.
First this my folder structure:
monsite.dev
classes
Database.inc
Users.inc
UsersProfile.inc
core
web
tester.php
Database.inc
to connect to my database
web/Index.php
for home page
tester.php
just for testing thing
I use require_once
to link file together, which works almost very good except for the tester.php
, which keeps telling me "failed to open the stream".
I tried 2 different way in tester.php
:
1 - require_once '../classes/database.inc';
(failed to open Database.inc)
2 - require_once 'Users/Nicks/Sites/monsite/classes/database.inc';
(failed to open Config.inc)
But when I use it from index.php
(require_once ../classes/database.inc) it works perfectly.
Why is this not working from tester.php
while it works from index.php
?
Many thanks for any help.
Nic
require_once 'classes/Database.inc';
您可以使用此文件,因为文件位于同一目录中。
That is because that is the incorrect path. In tester.php
it should be:
require_once __DIR__ . "/classes/Database.inc";
Using ../
goes back up a level. Therefore in your directory structure, you're saying that the classes/
directory is alongside the monsite.dev
directory. Not inside it.
When used in index.php
, which is in a subdirectory, you have to go back up a level - which is why it works from there.
tester.php
& classes
directory is on same folder so no need of ../
. Try with -
require_once 'classes/Database.inc';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.