【问题标题】:XAMPP Local installation File Path problemsXAMPP本地安装文件路径问题
【发布时间】:2012-03-04 02:42:28
【问题描述】:

我喜欢在上传到我的主机之前在本地网站上工作。我在 XAMPP 安装中使用 PHP/MYSQL 服务器。

我在 XAMPP htdocs 目录中有多个目录(每个项目一个)。每个项目通常至少有:

  1. header.php
  2. index.php
  3. footer.php
  4. styles/stylesheet.css

直到最近都还不错。

我现在正在研究更广泛的文件/目录结构。现在,当 /about/index.php 调用 header.php 时,样式表目录的路径没有指向正确的方向。图像路径也不再指向正确的位置,因为它们都是相对路径。

我尝试首先在每个路径的开头使用“/”将所有内容指向主目录,但在 XAMPP 中,主目录现在指的是 localhost,而不是特定项目的目录。

解决办法是什么?有没有更好的方法在本地处理项目,以便我可以使用所有相对路径简单地上传到我的网络主机,而不必为网站的实时版本和开发版本更改它们?

【问题讨论】:

    标签: php xampp relative-path directory-structure


    【解决方案1】:

    @Vladimir Dimitrovthis thread 中回答的最简单的解决方案如下(我将在这里复制):

    最简单的方法是为 /htdocs 中的每个站点文件夹创建单独的虚拟主机,因此您将访问 http://mysite.local 而不是 http://localhost/mysite

    有两件事要做:1. 编辑 C:\xampp\apache\conf\extra\httpd-vhosts.conf(默认)添加类似的内容:

    <VirtualHost *:80>
    ServerName      mysite.local
    DocumentRoot    C:/XAMPP/htdocs/mysite
    </VirtualHost>
    
    1. 编辑 c:\windows\system32\drivers\etc\hosts 添加

      127.0.0.1 mysite.local

    重启xampp并尝试http://mysite.local

    【讨论】:

      【解决方案2】:

      这可能对您有帮助吗? 您必须编辑一些配置以将每个站点引用到基本链接。 creating-multiple-sites-on-a-local-web-server

      【讨论】:

        【解决方案3】:

        你可以试试这个:

        • 创建通用配置文件
        • 为您的主目录定义一个 BASE_URL 常量(例如http://localhost/my_project/
        • 在您的模板中使用您的所有链接和引用与 BASE_URL

        部署时,您只需更改一个文件。

        您还可以为您的目录设置一个 BASE_PATH 常量(例如 c:/xampp/htdocs/my_project)。这在尝试包含来自各种子目录的脚本时可能很有用,而无需“猜测”本地路径。 (例如,包括 BASE_PATH . 'templates/my_template.php')

        【讨论】:

        • 如何创建通用配置文件?
        • 您创建一个文件,例如config.php 然后将该文件包含在顶部的所有脚本中:include 'config.php
        【解决方案4】:

        尝试使用 `DOCUMENT_ROOT 将它们包含在您的 PHP 文件中,即:

        include($_SERVER['DOCUMENT_ROOT']."folder/header.php");
        

        这假设,在浏览器中查看时,header.php 可以通过 http://127.0.0.1/folder/header.php 找到

        对于其他文件,例如 CSS、Javascript,您可以按如下方式定义位置:

        define("SCRIPTS_URL", "http://127.0.0.1/_scripts/");
        

        在你的 header.php 文件中包含上述内容,并确保在调用实际的 html 标头之前包含 header.php,例如:

        <?php
        include($_SERVER['DOCUMENT_ROOT']."folder/header.php");
        ?>
        <html>
            <link rel="stylesheet" type="text/css" href="<?php echo SCRIPTS_URL; ?>stylesheet.css">
            ... etc etc ...
        

        您可以进一步组合define并建立目录部分,例如:

        $project = "project_x";
        include($_SERVER['DOCUMENT_ROOT'].$project."/header.php");
        define("SCRIPTS_URL", "http://127.0.0.1/".$project."/_scripts/");
        

        如果你像上面那样做,那么你只需要改变项目变量,如果你看到...

        更新

        下面是 index.php:

        <?php
        // Make the header relative to index.php (as we don't know the project) - assume header is located at /_template/header.php and this file is located at /index.php [if, in future you have /content/index.php - then the below would be ../_template/header.php, etc]
        if(file_exists("_template/header.php")){
            include_once("_template/header.php");
        } else {
            die('Fatal error - no header found');
        }
        
        ?>
        <html>
        <head>
            <link rel="stylesheet" type="text/css" href="<?php echo BASE_URL; ?>styles/stylesheet.css">
        </head>
        <body>
        // Content goes here
        </body>
        </html>
        <?php
        
        if(file_exists(ROOTPATH."_template/footer.php")){
            include_once(ROOTPATH."_template/footer.php");
        }
        ?>
        

        还有header.php:

        <?php
        define("PROJECT_NAME", "project_x");
        define("ROOTPATH", $_SERVER['DOCUMENT_ROOT'].PROJECT_NAME."/");
        define("BASE_URL", "http://".$_SERVER['SERVER_NAME']."/".PROJECT_NAME."/"); // $_SERVER['SERVER_NAME'] automatically puts 'localhost' or the domain name in automatically
        ?>
        

        如您所见 - 所有内容都在此头文件中定义,当它包含在 index.php 中时 - index.php 可以访问这些定义,就像在定义完成后包含的任何其他文件一样(请注意您不能覆盖一个定义,也不能两次定义相同的定义。

        【讨论】:

        • 当我将它上传到 Bluehost 时,127.0.0.1 仍然有效吗?问题是 DOCUMENT_ROOT 在我的 XAMPP 安装和 Bluehost 中的基本 public_html 文件夹上是 localhost/。
        • 我在 header.php 的顶部添加了define("BASE_URL", "http://localhost/001_Current_Projects/".$project."/");。问题是,我的其余文件使用include('header.php') 作为第一行调用header.php。我可以在每个 php 文件的开头添加定义 base_url 代码,但是如何在我的包含代码中包含 BASE_URL?
        • 127.0.0.1 与 localhost 相同 - 将这些更改为您的域名。最好在使用模板系统时在整个脚本中调用一次标头,否则每次都必须进行大量更改 - 但是您可以在需要时使用这些定义的变量(假设 header.php 已经在调用该特定页面时包含)
        • 当我有多个页面并且每个页面都需要header.php 时,如何只调用一次header.php?感谢您的耐心和帮助,我对这一切真的很陌生。
        • 见上面的更新,如果你的标题是一个模板,你可以随时将header.php重命名为config.php
        【解决方案5】:

        我通过定义一些常量来解决这个问题:

        #   index.php
        #    handles pretty much everything for the site
        define( 'DS', DIRECTORY_SEPARATOR );
        define( 'ROOT', dirname(__file__) );
        define( 'HOST', 'http://' . $_SERVER['HTTP_HOST'] . dirname( $_SERVER['PHP_SELF'] ) );
        

        然后,对于包含,我会这样做:

        include ROOT . DS . 'directory' . DS . 'file_i_want.php';
        

        对于 CSS 等,设置 base URL in the markup 可能更容易。

        【讨论】:

          猜你喜欢
          • 2011-09-25
          • 1970-01-01
          • 2013-10-06
          • 2016-12-01
          • 1970-01-01
          • 1970-01-01
          • 2011-07-19
          • 2023-03-17
          • 1970-01-01
          相关资源
          最近更新 更多