XAMPP本地安装文件路径问题


XAMPP Local installation File Path problems

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

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

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

直到最近,这一切都很顺利

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

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

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

@Vladimir Dimitrov在这个线程中回答的最简单的解决方案如下(我将在这里复制它):

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

有两件事要做:1。编辑C:''examplep''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.本地

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

这可能对你有帮助吗?你必须编辑一些配置来引用每个网站的基本链接。创建本地web服务器上的多个站点

你可以试试这个:

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

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

您也可以在目录中设置BASE_PATH常量(例如c:''examplep/htdocs/my_project)。当尝试包含来自不同子目录的脚本时,这可能很有用,而无需"猜测"本地路径。(例如,包括BASE_PATH."templates/my_template.php")

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

include($_SERVER['DOCUMENT_ROOT']."folder/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中时,它可以访问这些定义,定义完成后包含的任何其他文件也可以访问(请注意,您不能覆盖一个定义,也不能两次定义同一定义。

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

#   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'] ) );

然后,对于includes,我做这样的事情:

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

对于CSS之类的东西,只需在标记中设置基本URL可能会更容易。