require_once中的相对路径不起作用 [英] relative path in require_once doesn't work

查看:369
本文介绍了require_once中的相对路径不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下结构

otsg
 > class
   > authentication.php
   > database.php
   > user.php
 > include
   > config.inc.php
   > encryption.php
   > include.php
   > session.php
 > index.php
 > registration.php

include.php文件具有以下

include.php file has the following

ini_set('display_errors', 1);
error_reporting(E_ALL);

ini_set('include_path',ini_get('include_path').':/Applications/MAMP/htdocs/otsg/:');
require_once 'config.inc.php';
require_once '../class/database.php';
require_once '../class/user.php';
require_once 'encryption.php';
require_once 'session.php';
require_once '../class/authentication.php';

,并且在我包含的index.php页面中

and in the index.php page I had included

require_once 'include/include.php';

当我打开页面index.php时,出现以下警告和致命错误.我不明白是什么原因导致此错误.当我给出绝对路径时,它可以工作.但是我相信绝对的路线不是一个好主意.

When I open the page index.php I get the following warning and fatal error. I don't understand what causes this error. When I gave the absolute path it works. But absolute path is not a good idea i believe.

Warning: require_once(../class/database.php) [function.require-once]: failed to open stream: No such file or directory in /Applications/MAMP/htdocs/otsg/include/include.php on line 9

Fatal error: require_once() [function.require]: Failed opening required '../class/database.php' (include_path='.:/Applications/MAMP/bin/php5.3/lib/php:/Applications/MAMP/htdocs/otsg/include/:') in /Applications/MAMP/htdocs/otsg/include/include.php on line 9

预先感谢

推荐答案

使用

__DIR__

获取脚本的当前路径,这应该可以解决您的问题.

to get the current path of the script and this should fix your problem.

所以:

require_once(__DIR__.'/../class/user.php');

这将防止您可以从其他文件夹运行PHP脚本,因此亲戚路径不起作用的情况.

This will prevent cases where you can run a PHP script from a different folder and therefore the relatives paths will not work.

斜杠问题已解决

这篇关于require_once中的相对路径不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆