[英]include_once(): failed to open stream: No such file or directory - PHP
I was trying to make a simple signup system in php.我试图在 php 中制作一个简单的注册系统。 I have a class called Admin(inside controller folder) which extends the DBConnection.php class.
我有一个名为 Admin(在控制器文件夹内)的类,它扩展了 DBConnection.php 类。 Admin class have a signup method for letting adming signup but got problem there.
Admin 类有一个注册方法可以让 adming 注册,但在那里遇到了问题。 Error occurs at 'include_once' and error says 'Warning: include_once(../Database/DBConnection.php): failed to open stream: No such file or directory in C:\\xampp\\htdocs\\WoodlandsAway\\controller\\Admin.php on line 15----- ' 'Warning: include_once(): Failed opening '../Database/DBConnection.php' for inclusion (include_path='C:\\xampp\\php\\PEAR') in C:\\xampp\\htdocs\\WoodlandsAway\\controller\\Admin.php on line 15----- ' 'Fatal error: Class 'DBConnection' not found in C:\\xampp\\htdocs\\WoodlandsAway\\controller\\Admin.php on line 17'
错误发生在 'include_once' 和错误说 '警告:include_once(../Database/DBConnection.php): 无法打开流: C:\\xampp\\htdocs\\WoodlandsAway\\controller\\Admin.php 上没有这样的文件或目录第 15 行 ----- ' '警告:include_once():无法在 C:\\xampp\\htdocs 中打开 '../Database/DBConnection.php' 以包含 (include_path='C:\\xampp\\php\\PEAR') \\WoodlandsAway\\controller\\Admin.php 第 15 行-----' '致命错误:第 17 行的 C:\\xampp\\htdocs\\WoodlandsAway\\controller\\Admin.php 中找不到类 'DBConnection''
here is my include_once code这是我的 include_once 代码
include_once ('../Database/DBConnection.php');
And here is my folder structure这是我的文件夹结构
--DBConnection.php --DBConnection.php
class DBConnection {
//put your code here
private $host;
private $user;
private $pass;
private $database;
private $conn;
function DBConnection() {
$this->host = 'localhost';
$this->user = 'root';
$this->pass = '';
$this->database = 'woodlands_away';
}
public function getConnections() {
$this->conn = new mysqli($this->host, $this->user, $this->pass, $this->database) or
die($this->conn->error);
return $this->conn;
}
} }
And Admin.php和 Admin.php
include_once ('../Database/DBConnection.php');
class Admin extends DBConnection {
public function Admin() {
parent::DBConnection();
}
public function signup($username, $password) {
$sql = "insert into users values(".$username.", ".$password.")";
return $this->getConnections()->query($sql);
}}
First, I suggest you to declare a constant that represents the root path of your project.首先,我建议您声明一个代表项目根路径的常量。 This constant must be declared in a unique way such an index.php or similar, but in the root of your project:
这个常量必须以一种独特的方式声明,比如 index.php 或类似的,但在你的项目的根目录中:
define('PROJECT_ROOT_PATH', __DIR__);
Then your include call should looks like this:然后你的包含调用应该是这样的:
include_once (PROJECT_ROOT_PATH . '/Database/DBConnection.php');
(Always specify the leading slash) (始终指定前导斜杠)
The problem is that currently your code may rely on the Working directory so you probably get an unexpected working directory.问题是目前您的代码可能依赖于工作目录,因此您可能会得到一个意外的工作目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.