【问题标题】:PHP require file from rootPHP需要来自根目录的文件
【发布时间】:2016-05-20 13:42:02
【问题描述】:

我尝试了有关此主题的各种其他主题,但没有成功。所以我写了我的具体案例。我再说一遍,我已经尝试过其他线程,但尝试其他人建议我没有得到结果。

所以,我正在创建自己的小 cms,不是很有经验。 我的结构是这样的:

/includes/config.php (class config.php) 
/index.php 
/other.php

现在我想在一个文件夹中创建一个管理区域:

**/admin**

然后我会调用 config.php 文件以构建查询和其他所有内容,但我不能。我试过这个:

require_once $_SERVER['DOCUMENT_ROOT'] 。 '/includes/config.php';

但它不起作用,我该如何解决?

编辑:

包括/config.php

class db {

private $conn;
private $host;
private $user;
private $password;
private $baseName;
private $port;
private $Debug;
    private $stmt;

    function __construct($params=array()) {
    $this->conn = false;
    $this->host = 'localhost'; //hostname
    $this->user = ''; //username
    $this->password = ''; //password
    $this->baseName = ''; //name of your database
    $this->port = '3306';
    $this->debug = true;
    $this->connect();
}

function __destruct() {
    $this->disconnect();
}

function connect() {
    if (!$this->conn) {
        try {
            $this->conn = new PDO('mysql:host='.$this->host.';dbname='.$this->baseName.'', $this->user, $this->password, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));  
        }
        catch (Exception $e) {
            die('Error : ' . $e->getMessage());
        }

        if (!$this->conn) {
            $this->status_fatal = true;
            echo 'Connection DB failed';
            die();
        } 
        else {
            $this->status_fatal = false;
        }
    }

    return $this->conn;
}

function disconnect() {
    if ($this->conn) {
        $this->conn = null;
    }
}


    public function query($query){
        $this->stmt = $this->conn->prepare($query);
    }


    public function bind($param, $value, $type = null){
            if (is_null($type)) {
                    switch (true) {
                        case is_int($value):
                            $type = PDO::PARAM_INT;
                            break;
                        case is_bool($value):
                            $type = PDO::PARAM_BOOL;
                            break;
                        case is_null($value):
                            $type = PDO::PARAM_NULL;
                            break;
                        default:
                            $type = PDO::PARAM_STR;
                    }
            }
        $this->stmt->bindValue($param, $value, $type);
    }

    public function execute(){
            return $this->stmt->execute();
    }        

    public function resultset(){
            $this->execute();
        return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
    }        

    public function single(){
            $this->execute();
        return $this->stmt->fetch(PDO::FETCH_ASSOC);
    }

    public function rowCount(){
        return $this->stmt->rowCount();
    }

    public function lastInsertId(){
       return $this->conn->lastInsertId();
    }

    public function beginTransaction(){
        return $this->conn->beginTransaction();
    }

    public function endTransaction(){
        return $this->conn->commit();
    }

    public function cancelTransaction(){
        return $this->conn->rollBack();
    }

    public function debugDumpParams(){
        return $this->conn->debugDumpParams();
    }

}

$database = 新数据库(); $pdo =& $数据库;

【问题讨论】:

  • ../includes/config.php 来自/admin/
  • 不,.. 表示您将返回上一个文件夹,对您来说,这是父文件夹,因此您可以访问配置文件。

标签: php


【解决方案1】:

如果您来自 /admin 文件夹,您可以简单地使用 ../includes/config.php 上一个文件夹,两个点表示您上一个目录,然后是目录“includes”,然后是config.php 文件。

另一种解决方案可以使用 .htaccess 文件(如果您使用 Apache)将所有请求路由到 1 个文件,例如您的 index.php 文件。这样,您总是来自同一个文件并从那里路由您的应用程序。

【讨论】:

  • 我怀疑我的类可能有任何断开的链接,因为根目录中的文件以及其中的所有类都被正确调用,但是如果像我的示例一样,我发现自己在 admin 文件夹中,无法正确调用所有内容。我正在发布文件 config.php 的代码
  • 您遇到了什么错误?您的 config.php 似乎没有任何问题。
  • 页面无法连接,HTTP ERROR 500。
  • 您好像关闭了错误报告。您可以通过输入 error_reporting(E_ALL); 在 PHP 中启用错误报告吗?在您的文件顶部或对报告进行快速谷歌搜索,您可能需要做一些事情来启用它。否则,您的服务器可能有一个 error.log 向您显示此处出错的确切内容。
【解决方案2】:

问题是微不足道的。在config.php我记得各种类,然后通过管理员路径是错误的,无法接他们。

示例:

include_once 'includes/user.php';
$users = new User($pdo);
$user =& $users;

我该如何解决?通过添加自动加载?我去过,但我不知道如何使用它.. @Millanzor

【讨论】:

    猜你喜欢
    • 2011-11-29
    • 2014-10-04
    • 2023-03-17
    • 2013-06-11
    • 1970-01-01
    • 2016-05-23
    • 2011-08-12
    • 2020-01-31
    相关资源
    最近更新 更多