PHP需要realpath - 需要打开失败 [英] PHP require realpath - Failed opening required
问题描述
我想通过相对路径 require
/ include
一个文件。存在路径并检查所有权限。要包含的脚本是可读的,可以通过浏览器中的URL访问...
I am trying to require
/include
a file by relative path. The paths exist and all rights are checked. The script to be included is readable and can be accessed via URL in browser...
但是,我收到的错误/警告无法打开。调用脚本和要包含的文件都在同一台服务器上。
However, I am receiving errors/warnings that it failed to open. Both the calling script and the file to be included are on the same server.
来电者亲属:
/devel/phi/dev/appcenter-head/appcenter/application/nm/index.php
来电者位置:
/ home / devel / wwwroot / phi / dev / appcenter-head / appcenter / application / nm
要求亲戚:
/ devel / nm / dev / http-api / http- api / src / httpapi.inc.php
需要位置:
/ home / devel / wwwroot / nm / dev / http-api / http-api / src
我试过的:
$strHttpApiUrl = "/devel/nm/dev/http-api/http-api/index.php";
$strFile = "/src/httpapi.inc.php";
$strDirName = dirname( $strHttpApiUrl );
$strRealpath = realpath( $strDirName . $strFile );
require_once $strRealpath;
失败: PHP警告:require_once(/ home / devel / wwwroot / phi / dev / appcenter-head / appcenter / application / nm)[< a href ='function.require-once'> functio
n.require-once< / a>]:无法打开流: /home/devel/wwwroot/phi/dev/appcenter-head/appcenter/application/nm/class/TWinLogin.php中没有这样的文件或目录
也失败了: PHP致命错误:require_once()[< a href ='function.require'> function.require< / a>]:无法打开所需''(include_path ='。:/ opt / nm / appcenter40-server-php53 / lib / php')
还尝试了包括字符串URL或使用绝对路径......
Also tried plain including the string URL or using absolute path...
如何包含该文件?
编辑:
这已成功包含以下文件:
This successfully includes the file:
require_once'/home/devel/wwwroot/nm/dev/http-api/http-api/src/httpapi.inc.php';
那么我怎样才能得到这条工作路径,假设我得到以下相对路径:
So how can I get that working path, assume I get the following relative path:
/ devel / nm / dev / http-api / http -api / index.php
推荐答案
realpath尝试在文件系统中获取真实路径 - 绝对路径。所以它最有可能以斜线开头。
realpath tries to get real path in file system -- absolute path. So it most possibly starts with slash.
如果你的结果路径以斜线开头而php试图要求/包含它,那么很可能会认为它是'绝对的。如果它没有看到现有文件 - 它会给你这些错误。
If your resulting path starts with slash and php tries to require/include it, most possibly it will think that its' absolute. And if it not see there existing file -- it gives you those error.
如果你想使用相对路径,请避免使用realpath。
If you want to use relative pathes, avoid realpath.
这篇关于PHP需要realpath - 需要打开失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!