简体   繁体   中英

Can't get the right include path in PHP

I read some other related questions and thought this was the best answer:

set_include_path(get_include_path().PATH_SEPARATOR."/path/to/program/root");

However, I can't get it to the right directory and the error messages aren't helpful enough, so I'm lost. My user area directory (shared host) looks like this:

/home/linweb09/b/example.com-1050560306/user    # <-- this is my root

I was trying to re-organise my program so that all of the model files go in this directory:

{root}/program_name/library/

The web-root folder is this:

{root}/htdocs/

So I changed all of my includes to look like this:

set_include_path(get_include_path().PATH_SEPARATOR
                 ."/home/linweb09/b/example.com-1050360506/user");
require_once "/program_name/library/some_module.php";

The index has to load this include to work, to authenticate the user:

/htdocs/admin/authenticate.php
# index.php
set_include_path(get_include_path().PATH_SEPARATOR
                 ."/home/linweb09/b/example.com-1050360506/user");
require_once "/htdocs/admin/authenticate.php";

And I run into these errors:

[warn] mod_fcgid: stderr: PHP Fatal error:
require_once() [function.require]: Failed opening required '/htdocs/admin/authenticate.php'
(include_path='.:/usr/share/pear:/usr/share/php:/home/linweb09/b/example.com-1050360506/user')
in /home/linweb09/b/example.com-1050360506/user/htdocs/admin/index.php on line 3

[warn] mod_fcgid: stderr: PHP Warning:
require_once(/htdocs/admin/authenticate.php) [function.require-once]: failed to open stream:
No such file or directory in
/home/linweb09/b/example.com-1050360506/user/htdocs/admin/index.php on line 3

But it is in the correct location and the error says no such file or directory, so I'm not sure how I'm supposed to configure this.

I also tried these and I got the same errors:

set_include_path(get_include_path().PATH_SEPARATOR."/user");
set_include_path(get_include_path().PATH_SEPARATOR."/");

Try removing the leading slash. /htdocs/admin/authenticate.php implies that you are working from root / . If you want it to search the include path, you need to use relative paths:

require_once "program_name/library/some_module.php";
require_once "htdocs/admin/authenticate.php";

Incidentally, you would only need to set the include path once per script - you dont need the set_include_path() line for every include, just once at the top of the script.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM