php - Reading from file (require) -
i have this: http://i.imgur.com/kpulybg.png , im working on "admin" folder , inside of have "admin.php", problem want read "core/init.php" there. have 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'; ?>
and inside "includes/header.php" have php require_once 'core/init.php'; admin page:
warning: require(core/init.php): failed 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
i know have add ../
error on index.php page must run without because not inside folder, runs header , footer includes folder.
as documentation explains:
files included based on file path given or, if none given,
include_path
specified....
if path defined — whether absolute (starting drive letter or \ on windows, or / on unix/linux systems) or relative current directory (starting . or ..) —
include_path
ignored altogether.
how applies code?
require_once 'core/init.php';
- php searches paths php.ini
directive include_path
. appends core/init.php
each path list , checks if path computed way exists. doesn't.
require_once './core/init.php';
- include_path
doesn't matter; provided relative path (core/init.php
) appended current directory path of file;
what's solution?
none of above ways works in practice.
the safest method include files using subdirectories use magic constant __dir__
, function dirname()
compute correct file path.
require '../includes/header.php';
becomes
require dirname(__dir__).'/includes/header.php';
and
require_once 'core/init.php';
becomes
require_once dirname(__dir__).'/core/init.php';
because __dir__
directory current file (includes/header.php
) located.
Comments
Post a Comment