【发布时间】:2014-02-26 21:04:24
【问题描述】:
我需要这个脚本来做一些非常简单的事情。
我从本地主机工作,然后上传。我需要返回的目录是动态的。即使它有多个子文件夹,它也能获取正确的 url。
上面写着
$baseurl = $protocol 。 $主机。 "/websites/andrewhunter/";
我需要的是,如果您正在下载或上传网站,脚本知道正确的 url。现在用于托管环境的是一个子目录。所以 domain.com/websites/client_website 这不能改变。
所以基本上我无法让它正常工作。我尝试了一些方法,这是通过在所有需要的页面上回显基本 url 的封闭方法。
不抓取上面的文件夹或者动态调整到domain.com/websites/我要手动添加
/* First we need to get the protocol the website is using */
$protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"], 0, 5)) == 'https' ? 'https://' : 'http://';
/* returns /myproject/index.php */
$path = $_SERVER['PHP_SELF'];
/*
* returns an array with:
* Array (
* [dirname] => /myproject/
* [basename] => index.php
* [extension] => php
* [filename] => index
* )
*/
$path_parts = pathinfo($path);
$directory = $path_parts['dirname'];
/*
* If we are visiting a page off the base URL, the dirname would just be a "/",
* If it is, we would want to remove this
*/
$directory = ($directory == "/") ? "" : $directory;
/* Returns localhost OR mysite.com */
$host = $_SERVER['HTTP_HOST'];
$baseurl = $protocol . $host . "/websites/andrewhunter/";
?>
【问题讨论】:
-
在远程服务器上运行脚本时,$path_parts 的内容是什么?根据网络服务器的配置,$_SERVER['PHP_SELF'] 可能不是您要查找的。我建议也检查 $_SERVER['SCRIPT_URL'] 的内容。
-
你可以查看这个链接,也许这就是你所需要的。 stackoverflow.com/questions/8399720/…