php require_once 中的相对路径不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5371828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:12:11  来源:igfitidea点击:

relative path in require_once doesn't work

phpwarningsfatal-errorrequire-once

提问by user525146

I have the following structure

我有以下结构

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

include.php file has the following

include.php 文件有以下内容

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';

and in the index.php page I had included

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

require_once 'include/include.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.

当我打开页面 index.php 时,我收到以下警告和致命错误。我不明白是什么导致了这个错误。当我给出绝对路径时,它就起作用了。但我相信绝对路径不是一个好主意。

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

Thanks in advance

提前致谢

回答by Raisen

Use

__DIR__

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

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

So:

所以:

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

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

这将防止您可以从不同的文件夹运行 PHP 脚本的情况,因此相关路径将不起作用。

Edit: slash problem fixed

编辑:斜线问题已修复

回答by Mobile Tech.

for php version 5.2.17 __DIR__will not work it will only works with php 5.3

对于 php 版本 5.2.17__DIR__将不起作用,它仅适用于 php 5.3

But for older version of php dirname(__FILE__)perfectly

但是对于旧版本的php dirname(__FILE__)完美

For example write like this

例如这样写

require_once dirname(__FILE__) . '/db_config.php';

回答by linuxatico

In my case it doesn't work, even with __DIR__or getcwd()it keeps picking the wrong path, I solved by defining a costant in every file I need with the absolute base path of the project:

在我的情况下,它不起作用,即使有__DIR__getcwd()它一直选择错误的路径,我通过在我需要的每个文件中使用项目的绝对基本路径定义一个 costant 来解决:

if(!defined('THISBASEPATH')){ define('THISBASEPATH', '/mypath/'); }
require_once THISBASEPATH.'cache/crud.php';
/*every other require_once you need*/

I have MAMP with php 5.4.10 and my folder hierarchy is basilar:

我有 php 5.4.10 的 MAMP,我的文件夹层次结构是基本的:

q.php 
w.php 
e.php 
r.php 
cache/a.php 
cache/b.php 
setting/a.php 
setting/b.php

....

....

回答by hamish

I just came across this same problem, where it was all working fine, up until the point I had an includes within another includes.

我刚刚遇到了同样的问题,在那里一切正常,直到我在另一个包含中包含一个包含。

require_once '../script/pdocrud.php';  //This worked fine up until I had an includes within another includes, then I got this error:
Fatal error: require_once() [function.require]: Failed opening required '../script/pdocrud.php' (include_path='.:/opt/php52/lib/php')

Solution 1.(undesired hardcoding of my public html folder name, but it works):

解决方案 1.(对我的公共 html 文件夹名称进行了不需要的硬编码,但它有效):

require_once $_SERVER["DOCUMENT_ROOT"] . '/orders.simplystyles.com/script/pdocrud.php';

Solution 2.(undesired comment above about DIR only working since php 5.3, but it works):

解决方案 2.(上面关于 DIR 仅在 php 5.3 后才有效,但它有效)的不受欢迎的评论:

require_once __DIR__. '/../script/pdocrud.php';

Solution 3.(I can't see any downsides, and it works perfectly in my php 5.3):

解决方案 3.(我看不出任何缺点,它在我的 php 5.3 中完美运行):

require_once dirname(__FILE__). '/../script/pdocrud.php';