PHP XAMPP 服务器 DOCUMENT_ROOT 文件夹结构
所以这是我第一次使用 xampp 创建测试站点.我最初将所有 php 文件放在一个文件夹中,最近才决定组织数据(是的,事后看来,我应该从有组织的文件夹结构开始.)无论如何,我的设置如下:
So this is my first time creating a test site with xampp. I originally had all my php files in one folder and just recently decided to organize the data (yes, hindsight i should've started with an organized folder structure.) Anyways, I have my setup like this:
" [ ] " 表示它是一个文件夹
" [ ] " implies it is a FOLDER
安装在我的 C: 驱动器上
Installed on my C: drive
[xampp]
└── [htdocs]
└── [QMS]
└── [rev3]
├── [css]
├── [js]
├── [DPU]
├── [login]
├── index.php
├── header.php
└── config.php
在我的config.php"文件中,我尝试定义一个根路径(这可能是错误):
In my "config.php" file I tried to define a root path (this may be the error):
$path = $_SERVER['DOCUMENT_ROOT'] . "/QMS/rev3/";
.
然后在我的 header.php 文件中我有:
Then in my header.php file I have:
<?php
require $_SERVER['DOCUMENT_ROOT'] . "/QMS/rev3/config.php";
include $_SERVER['DOCUMENT_ROOT'] . "/QMS/rev3/login/session.php";
.....
?>
HTML - located in the <head> section
<link rel='stylesheet' type='text/css' href='<?php echo $path . "css/searchBar.css"; ?>'/>
<link rel='stylesheet' type='text/css' href='<?php echo $path . "css/tables/filtergrid.css"; ?>'/>
<script type="text/javascript" language="javascript" src='<?php echo $path . "js/jquery.dataTables.js" ?>'></script>
<script type="text/javascript" language="javascript" src='<?php echo $path . "js/jquery.loader.js" ?>'></script>
... MANY OTHER scripts and stylesheets.
.我的 index.php 是:
. My index.php is:
require $_SERVER['DOCUMENT_ROOT'] . "/QMS/rev3/header.php";
当我在 Chrome 中启动它时 - 我的所有脚本和样式表都出现以下错误(总共 19 个错误):
When I launch this in Chrome - I get the following errors for ALL of my scripts and stylesheets (19 errors total):
"NOT ALLOWED TO LOAD LOCAL RESOURCE file///C:/xampp/htdocs/QMS/rev3/ ......etc..."
当我的所有文件都在同一个文件夹中并且我没有使用 SERVER['DOCUMENT_ROOT'] 时,我的网站运行良好,但现在我不知道该怎么办......有什么建议吗?
My site was working perfectly when all my files were in the same folder and I wasn't using SERVER['DOCUMENT_ROOT'], but now I have no idea what to do...any advice?
.
推荐答案
不允许加载本地资源
你在<a>
、<link>
、<script>
等标签中使用的url应该是相对的文档根目录,例如:
The url you use in <a>
, <link>
, <script>
, etc tags should be relative to the document-root, eg:
<link rel="stylesheet" type="text/css" href="/QMS/rev3/css/searchBar.css" />
不要将磁盘上的路径与url的混淆,它们是两个完全不同的东西:)
Don't confuse paths on disk with url's, they're two completely different things :)
建议确定根路径
我建议不要依赖 $_SERVER['DOCUMENT_ROOT']
变量,而是像这样确定根文件夹(在 config.php
中):
I recommend not to rely on the $_SERVER['DOCUMENT_ROOT']
variable, but determine the root-folder like this (in config.php
):
define('ROOT_PATH', __DIR__);
您将拥有一个名为 ROOT_PATH
的常量,其中将包含 C:xampphtdocsQMSev3
(没有尾随 /
).
You'll have a constant named ROOT_PATH
which will contain C:xampphtdocsQMSev3
(without a trailing /
).
现在您可以执行以下操作:
Now you can do things like:
require ROOT_PATH . '/header.php';
ROOT_PATH
中的路径与文档根目录不同.如果您真的想使用文档根目录,请执行此操作(这样 ROOT_PATH
将包含 C:xampphtdocs
):
The path in ROOT_PATH
differs from the document-root. If you really want to use the document-root, do this (so that ROOT_PATH
will then contain C:xampphtdocs
):
define('ROOT_PATH', __DIR__ . '/../..');
# ...
require ROOT_PATH . '/QMS/rev3/header.php';
规范化的绝对路径名
使用 Capsule 中提到的 realpath()
可能是明智的(展开所有符号链接和解析对 /./
、/../
和额外 /
字符的引用):
It might be wise to use realpath()
as mentioned by Capsule (expand all symbolic links and resolve references to /./
, /../
and extra /
characters):
$rootPath = __DIR__ . '/../..'; # or just __DIR__
$realPath = realpath($rootPath);
define('ROOT_PATH', $realPath ?: $rootPath);
如果 realpath()
无法解析路径,它将返回 false
,这就是为什么要进行一些检查.
If realpath()
fails to resolve the path it will return false
, that's why there's a little check.
相关文章