开发者

Problem including a file in PHP via absolute path

开发者 https://www.devze.com 2023-04-01 01:48 出处:网络
I\'m having a trouble deploying my website. I\'ve been building it under Windows, with WAMP and now I\'m deploying to Linux.

I'm having a trouble deploying my website. I've been building it under Windows, with WAMP and now I'm deploying to Linux.

Code like this works on Wi开发者_StackOverflow中文版ndows:

req.php

echo "This is req<br>";
ini_set("include_path", "/home/clash/public_html/:".get_include_path());
require "/req1.php";

req1.php

echo "This is req1";

But fails in the production environment with the message:

Warning: require(/req1.php) [function.require]: failed to open stream: No such file or directory in /home/clash/public_html/req.php on line 5

Fatal error: require() [function.require]: Failed opening required '/req1.php' (include_path='/home/clash/public_html/:.:/usr/lib/php:/usr/local/lib/php') in /home/clash/public_html/req.php on line 5

The path in ini_set is the path of the actual location of the website on the server filesystem.

I wouldn't like to avoid absolute paths as a library that I use relies on them somewhat.

I am sure that I'm missing something very simple here, but I can't figure out what.

Thanks!


On Linux, / is the root of the filesystem, similar to C:\ on Windows. Remove the leading / and it will work as expected.


You can replace the hard-coded path separator with the PATH_SEPARATOR constant. That way you ensure that your value for include_path is cross-platform.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号