user2916637
user2916637

Reputation: 181

PHP Page not getting root directory

I have a page that is in a /restricted directory on my server. I have includes files in a directory /includes . These includes files contain styling, header, nav, footer, etc. My issue is I can't seem to get it to find the includes files from the page in /restricted. Below is some of my code as is in the /restricted directory.

<!DOCTYPE HTML>
<html>

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>PRS Network</title>

    <?php include('includes/public_head.php'); ?>

    <!-- Google Analytics Code Goes Below Here -->

    <!-- End Google Analytics Code -->

</head>

<body> <?php include_once("includes/analyticstracking.php") ?>
    <div id="outter">
    <div id="wrapper">

        <?php include('includes/public_header.php'); ?>

        <?php include('includes/main_nav.php'); ?>

        <hr noshade width="97%">

        <?php include('includes/social.php'); ?>

I've tried placing "/" in front of includes and it still will not find the includes file. When I try to load the page in a browser this is what I get:

Warning: include(includes/public_head.php) [function.include]: failed to open stream: No such file or directory in .../html/restricted/videoarchive.php on line 12

Warning: include() [function.include]: Failed opening 'includes/public_head.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in .../html/restricted/videoarchive.php on line 12

Warning: include_once(includes/analyticstracking.php) [function.include-once]: failed to open stream: No such file or directory in .../html/restricted/videoarchive.php on line 20

Warning: include_once() [function.include]: Failed opening 'includes/analyticstracking.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in .../html/restricted/videoarchive.php on line 20

Warning: include(includes/public_header.php) [function.include]: failed to open stream: No such file or directory in .../html/restricted/videoarchive.php on line 24

Warning: include() [function.include]: Failed opening 'includes/public_header.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in .../html/restricted/videoarchive.php on line 24

Warning: include(includes/main_nav.php) [function.include]: failed to open stream: No such file or directory in .../html/restricted/videoarchive.php on line 26

Warning: include() [function.include]: Failed opening 'includes/main_nav.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in .../html/restricted/videoarchive.php on line 26

Warning: include(includes/social.php) [function.include]: failed to open stream: No such file or directory in .../html/restricted/videoarchive.php on line 30

(Note: I've removed some text in the error I receive in the browser. But it looks like it is always looking in the /restricted directory, even when I add "/" before includes.)

Any help is much appreciated!

Thanks in advance.

Upvotes: 0

Views: 1628

Answers (3)

kchang4
kchang4

Reputation: 92

if /restricted and /includes are both on the same level (same directory), then all you need to do is, (When you are inside of /restricted)

call the php constant DIR, then wrap it in dirname(DIR), so you can get an ABSOLUTE PATH.

<?php  include_once dirname(__DIR__).'/includes/blah/blah/blah.php';

What you shoudl really do is define a constant like:

<?php
 define('ROOT_DIR', __DIR__);

save that in a file (CONSTANTS.php) in the ROOT of your directory. then include it in all files, so no matter where you are/ what ever directory your in, you can always call ROOT_DIR to get your directory root, then add on to that with the directory you want.

Upvotes: 1

David
David

Reputation: 218877

If I understand correctly, you have folders like this:

/path/to/website/
/path/to/website/restricted/
/path/to/website/includes/

When you use this within the restricted folder:

include_once("includes/analyticstracking.php")

It's looking for this:

/path/to/website/restricted/includes/analyticstracking.php

If you prepend a /, it's looking for this:

/includes/analyticstracking.php

Neither of those exist. You need to traverse up one directly with the .. folder notation:

include_once("../includes/analyticstracking.php")

Or, conversely, fully-qualify the path:

include_once("/path/to/website/includes/analyticstracking.php")

Either way, you need to reference the file relatively from the current working directory of the script, or absolutely from the root of the file system.

Upvotes: 0

Digital Chris
Digital Chris

Reputation: 6202

Assuming that includes and restricted are at the same directory level, you need to point one directory up to find them:

instead of:

<?php include('includes/public_header.php'); ?>

You would use:

<?php include('../includes/public_header.php'); ?>

Upvotes: 0

Related Questions