从文件读取(必填) [英] Reading from file (require)

查看:84
本文介绍了从文件读取(必填)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的内容: http://i.imgur.com/KPulyBg.png和即时通讯目前正在"admin"文件夹中,并且我里面有"admin.php",但问题是我想从那里读取"core/init.php".现在我在admin.php中有这个

I have something like this: http://i.imgur.com/KPulyBg.png and im currently working on "admin" folder and inside of that i have "admin.php", but the problem is that i want to read "core/init.php" from there. Now i have this in admin.php

<?php
require '../includes/header.php';
?>

<?php
$user = new User();
if(!$user->isLoggedIn()){
    Redirect::to(404);
}
else if(!$user->hasPermission('admin')){
    Redirect::to(404);
}
?>
<div id="content">

</div>

<?php
require '../includes/footer.php';
?>

在"includes/header.php"中,我有php require_once'core/init.php';但是我在我的管理页面上得到了这个

And inside the "includes/header.php" i have php require_once 'core/init.php'; but i get this for my admin page:

Warning: require(core/init.php): failed to open stream: No such file or directory in C:\xampp\htdocs\OOP\includes\header.php on line 2

Fatal error: require(): Failed opening required 'core/init.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\OOP\includes\header.php on line 2

我知道我必须添加../,但是随后我在index.php页面上收到该错误,该错误必须在没有该错误的情况下运行,因为它不在文件夹内,它只从包含文件夹运行页眉和页脚. >

I know i have to add ../ but then i get that error on my index.php page which must be run without that because its not inside the folder, it only runs header and footer from includes folder.

推荐答案

作为文档解释:

根据给定的文件路径包含文件,如果没有给出,则根据指定的include_path包含文件.

...

如果定义了路径(无论是绝对路径(在Windows上以驱动器号或\开头,在Unix/Linux系统上以/开头)还是相对于当前目录(以.或..开头),则include_path将被完全忽略.

If a path is defined — whether absolute (starting with a drive letter or \ on Windows, or / on Unix/Linux systems) or relative to the current directory (starting with . or ..) — the include_path will be ignored altogether.

这如何适用于您的代码?

require_once 'core/init.php';-PHP搜索php.ini指令include_path中的所有路径.它将core/init.php附加到列表中的每个路径,并检查以此方式计算出的路径是否存在.很有可能不是.

How this applies to your code?

require_once 'core/init.php'; - PHP searches all the paths from the php.ini directive include_path. It appends core/init.php to each path from the list and checks if the path computed this way exists. Most probably it doesn't.

require_once './core/init.php';-include_path无关紧要;所提供的相对路径(core/init.php)附加到当前目录以获取文件的路径;

require_once './core/init.php'; - include_path doesn't matter; the provided relative path (core/init.php) is appended to the current directory to get the path of the file;

以上任何一种方法在实际中都无法实际发挥作用.

None of the above ways actually works in practice.

使用子目录包含文件的最安全方法是使用魔术常量 __DIR__ 和函数 dirname() 来计算正确的文件路径.

The safest method to include files using subdirectories is to use the magic constant __DIR__ and the function dirname() to compute the correct file path.

require '../includes/header.php';

成为

require dirname(__DIR__).'/includes/header.php';

require_once 'core/init.php';

成为

require_once dirname(__DIR__).'/core/init.php';

因为__DIR__是当前文件(includes/header.php)所在的目录.

because __DIR__ is the directory where the current file (includes/header.php) is located.

这篇关于从文件读取(必填)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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