【发布时间】:2020-07-18 15:40:49
【问题描述】:
我正在创建一个在初始项目设置阶段提供动态数据库创建的 CMS。
我已将数据库配置详细信息定义为 config.php 文件中的常量:
//Database Name
define('DEFAULT_DB_NAME', 'cms');
//User Name
define('DEFAULT_USER_NAME', 'root');
//Password
define('DEFAULT_PASSWORD', '');
//Host Name
define('DEFAULT_HOST_NAME', 'localhost');
我在我的数据库连接文件 (db.class.php) 中包含了 config.php 文件:
<?php
include_once '../../config/config.php';
class Db{
protected $conn;
protected $host = DEFAULT_HOST_NAME;
protected $username = DEFAULT_USER_NAME;
protected $password = DEFAULT_PASSWORD;
protected $dbname = DEFAULT_DB_NAME;
public function __construct(){
$this->conn = new mysqli($this->host, $this->username, $this->password, $this->dbname);
if($this->conn->connect_error){
die("<h3>Connection Failed!!! Error: " . $this->conn->connect_error . "</h3>");
}
}
}
我还创建了一个动态样式表 (admintheme.php),它可以根据用户偏好修改管理面板:
<?php
header("Content-type: text/css;");
include_once '../../model/admintheme.class.php';
$theme = new Admintheme();
$result = $theme->ReadAdminTheme();
if($result == '' || $result == '0'){
$sidebarBg = "#111";
$sidebarPosition = "left";
$sidebarunset = "left";
$sidebarright = "unset";
}
else{
$row = $result->fetch_assoc();
$sidebarBg = $row['sidebarbg'];
$sidebarPosition = $row['sidebar_position'];
if($sidebarPosition == "left"){
$sidebarunset = "right";
}
else{
$sidebarunset = "left";
}
}
?>
/*-- ------------------------xx----------------------- */
/***** Content Section Starts *****/
.content{
margin-<?= $sidebarPosition; ?>: auto;
}
/***** Content Section Ends *****/
/**** Side Bar Section Starts *****/
.sidebar-nav{
background-color: <?= $sidebarBg; ?>;
<?= $sidebarPosition; ?>: 0px;
}
问题是当我在“db.class.php”文件中使用静态数据时(例如,直接写“localhost”代替常量等等..)然后“admintheme.php”工作正常并显示所需的输出,但是当我使用常量代替静态数据时,项目的所有其他功能都可以正常工作并从数据库中检索数据,但“admintheme.php”除外。
当常量在“db.class.php”中定义或“config.php”文件与“db.class.php”位于同一目录时,“admintheme.php”也可以正常工作但是在包含其他目录中的“config.php”文件时,会检索除“admintheme.php”之外的所有其他数据。
**没有收到直接错误,但在浏览器的控制台中,它指出 (include_once(../../config/config.php): failed to open stream: No such file or directory...)
**包含的路径在“db.class.php”和“admintheme.php”中都是正确的。
非常感谢任何帮助。
【问题讨论】:
-
您的路径错误。您正在根据当前的 php 环境设置调整包含路径(这可能因机器而异,以及您在 url 中点击的初始 php 的位置)。您确实应该使用
__DIR__作为基础,并从正在执行包含的文件中导航。通过这种方式,它会从您知道已将其与执行拉动的文件相关的位置拉动。 -
@IncredibleHat 感谢您的回复,我一定会考虑在项目中使用 DIR。但我使用 VSCODE 作为文本编辑器,它显示文件建议,同时包含它们我已经手动交叉验证了路径,但我没有发现任何问题。
-
嗯,你有一个问题很明显;)因为“没有这样的文件或目录”......意味着它在发生的路径遍历中找不到文件。跨度>
-
@IncredibleHat 添加 'DIR' 解决了问题.....感谢您的帮助 :)