Php 如何正确包含具有正确路径的文件并删除警告“;“文件不存在”;
我正在从事一个将连接到MySQL数据库的项目,该项目要求:Php 如何正确包含具有正确路径的文件并删除警告“;“文件不存在”;,php,include,htdocs,Php,Include,Htdocs,我正在从事一个将连接到MySQL数据库的项目,该项目要求: 登记 登录/登录/注销 家 链接 有两项可供登录者使用: 我的书签 联系我们 我目前的网站结构如下: *Shortened* URL (project root folder): sois.com/440/finalproject/ <?php # index.php session_start(); //check session first if (!isset($_SESSION['email'])){ incl
- 登记
- 登录/登录/注销
- 家
- 链接
- 我的书签
- 联系我们
*Shortened* URL (project root folder): sois.com/440/finalproject/
<?php # index.php
session_start();
//check session first
if (!isset($_SESSION['email'])){
include ('header.php');
}else
{
include ('header.php');
}
?>
<h2>Project Description</h2>
<h2>Blah Blah Blah ...</h2>
<h2>put images here if you want ...</h2>
<h2>change background ...</h2>
<h2>modify the site as you want ...</h2>
<?php
include ('footer.php');
?>
- 最终项目文件夹内容:htdocs文件夹| mysql_connect.php
- htdocs文件夹内容:书签文件夹|联系人文件夹|主文件夹|包含文件夹
- 书签文件夹内容(所有PHP文件):添加|删除|删除确认|索引|搜索|搜索表单|更新|更新表单
- 联系人文件夹内容:contact.php
- 主文件夹内容(所有PHP文件):忘记|索引|链接|登录|注销|注册
- 包括文件夹内容:footer.php | header.php | logo.jpg
*Shortened* URL (project root folder): sois.com/440/finalproject/
<?php # index.php
session_start();
//check session first
if (!isset($_SESSION['email'])){
include ('header.php');
}else
{
include ('header.php');
}
?>
<h2>Project Description</h2>
<h2>Blah Blah Blah ...</h2>
<h2>put images here if you want ...</h2>
<h2>change background ...</h2>
<h2>modify the site as you want ...</h2>
<?php
include ('footer.php');
?>
项目说明
废话废话。。。
如果你想的话,把图片放在这里。。。
更改背景。。。
根据需要修改网站。。。
我的问题很简单,我收到了错误:
- “警告:include(kethcart.uwmsois.com/public_html/440/finalproject/htdocs/includes/header.php):无法打开流:第5行的/home/kethcart/public_html/440/finalproject/htdocs/home/index.php中没有此类文件或目录”
- “警告:include():无法在第5行的/home/kethcart/public_html/440/finalproject/htdocs/includes/header.php中打开“kethcart.uwmsois.com/public_html/440/finalproject/htdocs/includes/header.php”以包含(include_path='。:/usr/lib/php:/usr/local/lib/php')”
- “警告:include(kethcart.uwmsois.com/public_html/440/finalproject/htdocs/includes/footer.php):无法打开流:第19行的/home/kethcart/public_html/440/finalproject/htdocs/home/index.php中没有此类文件或目录”
- “警告:include():无法在第19行的/home/kethcart/public_html/440/finalproject/htdocs/includes/footer.php中打开“kethcart.uwmsois.com/public_html/440/finalproject/htdocs/includes/footer.php”以包含(include_path='。:/usr/lib/php:/usr/local/lib/php')”
谢谢大家,如果您有任何进一步的问题或需要更多详细信息,请告诉我。当您像当前一样在PHP中包含文件时(只提供文件名,例如
header.PHP
),PHP将与当前执行的文件位于同一目录中
根据我对您的描述的理解(我很难理解格式),这是您在文档根目录中的目录布局(我只显示了对这个答案很重要的文件):
因此,如果您在/home/index.php
中,并且包含“header.php”
,php将假定您正在查找位于/home/header.php
的文件。您需要做的是告诉PHP您正在寻找一个不同的位置
在回答您的问题时,一个很好的方法是告诉PHP相对于当前目录查看。以下是您的Include之后的外观:
include '../includes/header.php';
include '../includes/footer.php';
这基本上告诉PHP从/home
(进入/
),进入includes
目录,并包括footer.PHP
或header.PHP
文件。您可以使用目录名(\uuuuuu文件\uuuu)代码>php函数-给定包含文件或目录路径的字符串,此函数将返回父目录的路径
$file_path = dirname(__FILE__);
include $file_path . DIRECTORY_SEPARATOR . 'header.php';
REF:您可以通过使用\uuuuu DIR\uuuuuu
获取当前脚本的路径(或者dirname(\uuuuu FILE\uuuuu)
如果DIR不可用)
现在,要包含一个文件,您可以执行以下操作:
<?php
define('ROOT', __DIR__.'/');
include (ROOT.'includes/header.php');
// or include relative to the root dir (if it's in a parent or sub dir)
include (ROOT.'../../header.php');
include (ROOT.'../includes/header.php');
// and if you want a "cleaner inclusion"
// the following will get the full absolute path without ".."
$path = realpath(ROOT.'../../header.php');
include ($path);
include('../includes/header.php')
如果你想在互联网上发布图片,下次你可以使用免费的图片托管,而不是谷歌硬盘(大多数都不需要账户),谢谢你们的回复。Asenar-我为图像托管问题道歉,Google Drive对我来说是最简单、即时的解决方案,因为我已经将它安装在我的桌面上,并且可以直接访问链接。我会记住你对未来帖子的建议。非常感谢你的精确解释,很抱歉格式问题-我一直使用StackOverflow解决我的编码问题,但直到昨晚才真正创建帐户,所以我仍然对textarea输入有感觉。再次感谢你。当我有机会的时候,我会发布我的更新代码。我在另一篇文章中看到了这一点,作为一个可能的解决方案,但我并不真正理解它到底是如何工作的。与(…include/header.php)方法相比,您认为该方法有什么特别的优势吗?优势在于,如果您让代码进化,那么如果您这样做(并添加其他常量,如LIBPATH、VIEWPATH等),它会更简单,因此,当您移动所有文件时,只需修改一小段代码即可。另一个优点是,即使您更改了include路径(set_include_path),这也会起作用。