PHP:包含和文件夹
我有一个小型PHP网站,其中包含我的大部分页面:PHP:包含和文件夹,php,include,directory,Php,Include,Directory,我有一个小型PHP网站,其中包含我的大部分页面: <?php include("heading.php"); ?> <!-- Content of the page --> <?php include("footing.php"); ?> 其中footting包含一些要放在每个文件末尾的内容,heading包含很多内容,包括菜单的另一个include。问题是:我开始有很多php文件,我想把其中一些放在文件夹中。最简单的方法是将文件夹中文件的代码更改为:
<?php include("heading.php"); ?>
<!-- Content of the page -->
<?php include("footing.php"); ?>
其中footting包含一些要放在每个文件末尾的内容,heading包含很多内容,包括菜单的另一个include。问题是:我开始有很多php文件,我想把其中一些放在文件夹中。最简单的方法是将文件夹中文件的代码更改为:
<?php include("../heading.php"); ?>
<!-- Content of the page -->
<?php include("../footing.php"); ?>
但是它不能像include那样以书面方式复制代码,而不是在原始文件的文件夹中执行代码,因此在heading.php中找不到任何include和css,除非我将这些文件复制到新文件夹中。修改heading.php,以便加载的文件的路径是绝对的。例如,如果从CSS/style.CSS加载CSS文件,请使用/CSS/style.CSS或http://example.com/css/style.css 改为。修改heading.php,使其加载的文件的路径是绝对路径。例如,如果从CSS/style.CSS加载CSS文件,请使用/CSS/style.CSS或http://example.com/css/style.css 相反。您还可以在php.ini中或直接在代码中编辑包含路径:
//Must be called before every include, if not stated in your php.ini
$PATH=get_include_path();
set_include_path($PATH.":/absolute/path/to/your/include/folder/");
然后,即使您位于子目录中,也可以通过这种方式使用include:
<?php
include "heading.php"; //no brackets
include "whatever.php";
?>
您还可以在php.ini中或直接在代码中编辑包含路径:
//Must be called before every include, if not stated in your php.ini
$PATH=get_include_path();
set_include_path($PATH.":/absolute/path/to/your/include/folder/");
然后,即使您位于子目录中,也可以通过这种方式使用include:
<?php
include "heading.php"; //no brackets
include "whatever.php";
?>
好的,严格来说,这不是你问题的答案,但试着用另一种方式来做,创建一个包含页眉和页脚的布局文件,并动态地包含主要内容
<html>
<head></head>
<body>
etc...
<?php include_once '/path/to/content.php'; ?>
etc...
</body>
</html>
好的,严格来说,这不是你问题的答案,但试着用另一种方式来做,创建一个包含页眉和页脚的布局文件,并动态地包含主要内容
<html>
<head></head>
<body>
etc...
<?php include_once '/path/to/content.php'; ?>
etc...
</body>
</html>
我不确定我是否正确理解你的问题,但我有以下答案 我认为你应该使用相对css路径到你的网站地址 例如,如果我们有一个css文件,它应该是这样的
<link ...src="/styles/somecss.css">
因此,无论您将它放在应用程序中的什么地方,这都将是一条路径
您可以将include路径设置为所需的任何目录
设置包含路径
希望这会有所帮助我不确定我是否正确理解您的问题,但我有以下答案 我认为你应该使用相对css路径到你的网站地址 例如,如果我们有一个css文件,它应该是这样的
<link ...src="/styles/somecss.css">
因此,无论您将它放在应用程序中的什么地方,这都将是一条路径
您可以将include路径设置为所需的任何目录
设置包含路径
希望这会有所帮助。听起来你只是不知道自己在目录结构中的位置。使用../返回一个级别,使用./or just/返回根目录,.././返回两个级别 诀窍是知道您在目录结构中的位置以及要包含的文件的位置。如果您正在css文件夹中包含或链接一个文件,但您位于inc文件夹中的一个文件中,则必须先返回根目录,然后再返回css,以便放置/css/filename.ext 但是,如果您在fonts/glyphicons文件夹中,并且希望链接到fonts/font awesome文件夹中的字体,则可以使用../font awesome。清澈如泥 我要做的是创建三个或更多文件夹。这有助于将各种文件组织起来,并使用缩写字母,即: 样式表的css,如bootstrap.css、style.css等。 图像、图形、图标等的img。 字体、字形图标、字体等的字体。 inc for includes/“snippets”如heading.php和footting.php js用于Java脚本,如html5shiv.js、boostrap.js等。 然后在我的heading.html文件中,我使用html作为页眉和页脚,我包括如下一些项目:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?php echo $page_title; ?></title>
<meta name="Description" content="<?php echo $page_description; ?>">
<meta name="Keywords" content="<?php echo $page_keywords; ?>">
<meta name="Author" content="<?php echo $author_name; ?>">
<meta name="Viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="/css/quicksite-redsand.css" rel="stylesheet">
<link href="/img/mm.png" type="image/x-icon" rel="shortcut icon">
<!--[if lt IE 9]><script src="/js/html5shiv.js"></script>
<script src="/js/respond.min.js"></script><![endif]-->
</head>
<body>
然后在我的页面(如index.php主页)中,我为该特定页面设置了一些参数,并包括一些片段,如页眉、菜单、其他正文文本项和页脚,如下所示:
<?php
$page_name = 'Homepage';
$page_author = 'My Name...';
$page_keywords = 'home, home page, homepage';
$page_description = 'This is the home page for the ...';
$active_home = 'class="active"';
$select_home = 'class="selected"';
include ('inc/config.php'); //common to all pages
include ('inc/header.html'); //common to all pages
include ('inc/menus.html'); //common to all pages
include ('inc/banner.html');
include ('inc/review.html');
include ('inc/footer.html'); //common to all pages
?>
因为在index.php和大多数页面中,您处于根级别,可以说是在主目录中,所以您只需要inc而不需要/。看起来PHP不喜欢不必要的/,因为它在我尝试时给了我一个错误
我知道这是一个冗长的答案,但我想我会说明更好的理解 听起来好像你只是不知道自己在目录结构中的位置。使用../返回一个级别,使用./or just/返回根目录,.././返回两个级别 诀窍是知道您在目录结构中的位置以及要包含的文件的位置。如果您正在css文件夹中包含或链接一个文件,但您位于inc文件夹中的一个文件中,则必须先返回根目录,然后再返回css,以便放置/css/filename.ext 但是,如果您在fonts/glyphicons文件夹中,并且希望链接到fonts/fon中的字体 t-awesome文件夹,您可以使用../font-awesome。清澈如泥 我要做的是创建三个或更多文件夹。这有助于将各种文件组织起来,并使用缩写字母,即: 样式表的css,如bootstrap.css、style.css等。 图像、图形、图标等的img。 字体、字形图标、字体等的字体。 inc for includes/“snippets”如heading.php和footting.php js用于Java脚本,如html5shiv.js、boostrap.js等。 然后在我的heading.html文件中,我使用html作为页眉和页脚,我包括如下一些项目:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?php echo $page_title; ?></title>
<meta name="Description" content="<?php echo $page_description; ?>">
<meta name="Keywords" content="<?php echo $page_keywords; ?>">
<meta name="Author" content="<?php echo $author_name; ?>">
<meta name="Viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="/css/quicksite-redsand.css" rel="stylesheet">
<link href="/img/mm.png" type="image/x-icon" rel="shortcut icon">
<!--[if lt IE 9]><script src="/js/html5shiv.js"></script>
<script src="/js/respond.min.js"></script><![endif]-->
</head>
<body>
然后在我的页面(如index.php主页)中,我为该特定页面设置了一些参数,并包括一些片段,如页眉、菜单、其他正文文本项和页脚,如下所示:
<?php
$page_name = 'Homepage';
$page_author = 'My Name...';
$page_keywords = 'home, home page, homepage';
$page_description = 'This is the home page for the ...';
$active_home = 'class="active"';
$select_home = 'class="selected"';
include ('inc/config.php'); //common to all pages
include ('inc/header.html'); //common to all pages
include ('inc/menus.html'); //common to all pages
include ('inc/banner.html');
include ('inc/review.html');
include ('inc/footer.html'); //common to all pages
?>
因为在index.php和大多数页面中,您处于根级别,可以说是在主目录中,所以您只需要inc而不需要/。看起来PHP不喜欢不必要的/,因为它在我尝试时给了我一个错误
我知道这是一个冗长的答案,但我想我会说明更好的理解 strightforward是使用absolute pathRight抱歉,在提交我的答案之前没有看到k102的注释。strightforward是使用absolute pathRight抱歉,在提交我的答案之前没有看到k102的注释。或者使用某种配置变量或函数,您可以说,最好有一个包含定义了css路径的文件,php和其他文件,以便快速更改,或者使用某种配置变量或函数,让您可以说,最好有一个包含css、php和其他文件的已定义路径的文件,以便更快地更改