gpt4 book ai didi

php - 获取基本路径/URL

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:54:56 24 4
gpt4 key购买 nike

我试图通过一个函数获取文档的基本路径,因为我不想找到像 ../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"],0,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.phpfunctions.php 都在 http://localhost/mysite/_include/db//p>

当我运行该页面时,错误显示...

Warning: require_once(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in C:\xampp\htdocs\mysite\_include\header.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:\xampp\htdocs\mysite\_include\header.php on line 9

Fatal error: require_once(): Failed opening required 'http://localhost/mysite/_include/db/qry.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\mysite\_include\header.php on line 9

我尝试像 echo $base; 那样回显 getBaseUrl(),它显示了正确的路径,即 http://localhost/mysite/

我该怎么办?

最佳答案

您可以使用 $_SERVER['DOCUMENT_ROOT']

关于php - 获取基本路径/URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24008825/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com