[英]php require in class file
I have the following folder structure for my site: 我的网站具有以下文件夹结构:
Admin/
Tech/
index.php
classes/
include/
core/
Everything seems to work except for when i try to call the following code from Admin/index.php 除了当我尝试从Admin / index.php调用以下代码时,所有内容似乎都可以正常工作
<?php
#including our init.php
require '../core/init.php';
$general->logged_in_protect();
?>
I get the following error for some reason. 由于某种原因,我收到以下错误。 I can't figure out what to do to fix it or if its even possible to fix. 我不知道该怎么办才能修复它,甚至无法修复。
The core/init.php file has the following code: core / init.php文件具有以下代码:
<?php
#starting the users session
session_start();
require 'include/database.php';
require 'classes/users.php';
require 'classes/general.php';
$users = new Users($db);
$general = new General();
$errors = array();
?>
error that i am receiving: 我收到的错误:
PHP Warning: require(include/database.php): failed to open stream: No such file or directory in
Can someone please help me with this? 有人可以帮我吗?
You probably just need: 您可能只需要:
require '../include/database.php';
require '../classes/users.php';
require '../classes/general.php';
But if you use a lot of relative paths, it can get messy real quickly so you might want to consider using your base-directory like for example (you can also use a variable or a constant for that): 但是,如果您使用大量相对路径,它很快就会变得混乱不堪,因此您可能需要考虑使用您的基本目录,例如(您也可以使用变量或常量):
require $_SERVER['DOCUMENT_ROOT'] . '/include/database.php';
require $_SERVER['DOCUMENT_ROOT'] . '/classes/users.php';
require $_SERVER['DOCUMENT_ROOT'] . '/classes/general.php';
If you use paths relative to current script location, like this: 如果您使用相对于当前脚本位置的路径,如下所示:
<?php
require(dirname(__FILE__).'/../core/other.inc');
...
?>
You always have it under control, regardless of the include_path
PHP ini setting. 无论include_path
PHP ini设置如何,您始终可以控制它。
In your case you should have the following: 在您的情况下,您应该具备以下条件:
<?php
#including our init.php
require(dirname(__FILE__).'/core/init.php');
$general->logged_in_protect();
?>
and core/init.php
will look like: 和core/init.php
将看起来像:
<?php
#starting the users session
session_start();
require(dirname(__FILE__).'/../include/database.php');
require(dirname(__FILE__).'/../classes/users.php');
require(dirname(__FILE__).'/../classes/general.php');
$users = new Users($db);
$general = new General();
$errors = array();
?>
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.