简体   繁体   中英

Correct path to file in LiteSpeed web server on Linux

I have the following PHP script placed in the root directory of my web hosting server, and it works fine.

<?php
include(dirname(__FILE__).'/config/config.inc.php');
require_once(dirname(__FILE__).'/init.php');

When I moved the script to a new folder under my web root, ie public_html/myfolder , the script gives the following error:

Warning: include(/home/name/public_html/folder/config/config.inc.php) [function.include]: failed to open stream: No such file or directory in /home/name/public_html/folder/test.php on line 2

Warning: include() [function.include]: Failed opening '/home/name/public_html/folder/config/config.inc.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/name/public_html/folder/test.php on line 2

Warning: require_once(/home/name/public_html/folder/init.php) [function.require-once]: failed to open stream: No such file or directory in /home/name/public_html/folder/test.php on line 3

Fatal error: require_once() [function.require]: Failed opening required '/home/name/public_html/folder/init.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/name/public_html/folder/test.php on line 3

Should I change the path as follows:

<?php
include(dirname(__FILE__).'/../config/config.inc.php');
require_once(dirname(__FILE__).'/../init.php');

Am I correct?

Uhm, you say you only moved your script (the one that contains the include lines) to the "public_html/folder". That way, dirname(__FILE__) contains the full path, including the "folder" directory. So, now, you need to go up one step in the directory tree, to be able to reach the "config" directory; an explanation follows:

Before you had:

/home/name/public_html/config/config.inc.php
/home/name/public_html/init.php

So your file

/home/name/public_html/test.php

Managed to access the files using the path from dirname(__FILE) . But now, the test.php has been movedto the directory "folder":

/home/name/public_html/folder/test.php

Thus, dirname(__FILE__) should return the path comprehensive of the "folder" directory. But the files you need to include are in teh "old" directory, so youneed to add that ../ to the path of the include, to be abletoreach those files :)

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