通过绝对路径在PHP中包含文件的问题
问题描述:
我在部署我的网站时遇到问题。我一直在使用WAMP在Windows下构建它,现在我正在部署到Linux。通过绝对路径在PHP中包含文件的问题
这样的代码在Windows上运行:
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";
但在生产环境中与消息失败:
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
ini_set中的路径是服务器文件系统上网站实际位置的路径。
我不想避免绝对路径,因为我使用的库在某种程度上依赖于它们。
我相信我在这里错过了一些非常简单的东西,但我无法弄清楚什么。
谢谢!
答
在Linux上,/
是文件系统的根,类似于Windows上的C:\
。删除前导/
,它会按预期工作。
答
可以用PATH_SEPARATOR
常量替换硬编码的路径分隔符。这样你可以确保你的价值是include_path
是跨平台的。
就像我说的那样,这是一个我不想留下来的选项,因为我使用的框架生成这种包含URL。有没有办法自动将它们与应用程序路径相加? – Corvin