【问题标题】:Dynamic URL Sub Folders动态 URL 子文件夹
【发布时间】: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/…

标签: php dynamic


【解决方案1】:

为什么是$path_parts?我对你的问题有点困惑,但我知道你基本上想要在浏览器的地址字段中写什么?这适用于我的遥控器以及我的本地 MAMP,文件可能位于我的文件夹中的第一级或第四级

 /* 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 localhost OR mysite.com */
$host = $_SERVER['HTTP_HOST'];

$baseurl = $protocol . $host . $path;

echo $baseurl;

【讨论】:

  • localhost/website/home/index.php/images/logo.jpg 就是它现在所做的。我需要的是让它现在动态地上升几个目录。例如“/website/images/logo.jpg”,但对于每个页面,甚至是像 /website/folder/sub-folder/ 这样的子页面
  • 所以主要问题是我需要这个: $baseurl = $protocol 。 $主机。 “/网站/客户端/”;但即使在页面上的导航、图像等都可以正常工作。我正在使用导入来修复这些样式表,它工作正常。所以我在每个子文件夹/website/client/folder/sub-folder/style.css 中有一个style.css(导入到真正的css)
  • 正在阅读 $path = substr( FILE, strlen( $_SERVER[ 'DOCUMENT_ROOT' ] ) ); $url = "www.mydomain.com{$path}";
猜你喜欢
  • 2021-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多