php – 获取基本路径/ URL
发布时间:2020-12-13 22:11:04 所属栏目:PHP教程 来源:网络整理
导读:我试图通过函数获取文档的基本路径,因为我不想找到像../folder1/folder2/mypage.php或../../../folder1/folder2/somepage这样的路径. PHP. 所以我试过…… function getBaseUrl() {// output: /myproject/index.php$currentPath = $_SERVER['PHP_SELF'];// o
我试图通过函数获取文档的基本路径,因为我不想找到像../folder1/folder2/mypage.php或../../../folder1/folder2/somepage这样的路径.
PHP.
所以我试过…… function getBaseUrl() { // output: /myproject/index.php $currentPath = $_SERVER['PHP_SELF']; // output: Array ( [dirname] => /myproject [basename] => index.php [extension] => php [filename] => index ) $pathInfo = pathinfo($currentPath); // output: localhost $hostName = $_SERVER['HTTP_HOST']; // output: http:// $protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],5))=='https://'?'https://':'http://'; // return: http://localhost/myproject/ return $protocol.$hostName.$pathInfo['dirname']."/"; } 然后我给写代码…… $base = getBaseUrl(); require_once $base.'_include/db/qry.php'; require_once $base.'_include/db/functions.php'; 两个文件qry.php& functions.php在http:// localhost / mysite / _include / db /中 当我运行页面时,错误显示… Warning: require_once(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in C:xampphtdocsmysite_includeheader.php on line 9 Warning: require_once(http://localhost/mysite/_include/db/qry.php): failed to open stream: no suitable wrapper could be found in C:xampphtdocsmysite_includeheader.php on line 9 Fatal error: require_once(): Failed opening required 'http://localhost/mysite/_include/db/qry.php' (include_path='.;C:xamppphpPEAR') in C:xampphtdocsmysite_includeheader.php on line 9 我尝试通过回显像echo $base这样的getBaseUrl();它显示正确的路径,即http:// localhost / mysite /. 我该怎么办 ? 解决方法
您应该只使用服务器上的绝对路径而不是url.
您可以使用__DIR__获取基本路径. 例如: // just example,change to fit your real path. $base = __DIR__ . '/../'; require_once $base.'_include/db/qry.php'; require_once $base.'_include/db/functions.php'; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |