2

I have a file structure as follows:

>project
 >admin
 >classes
  >class_main.php
 >templates
  >foot.php

Within class_main.php, I am trying to use require_once to require the same file, dependent on where the user is (ie, if the user is in the admin dir, require_once "../templates/foot.php" otherwise require_once "./templates/foot.php") inside of the __destruct function of my class.

My problem is that no matter what I try, I am always receiving the same error:

Warning: require_once(C:\wamp\www\project\classes/templates/foot.php) [function.require-once]: failed to open stream: No such file or directory in C:\wamp\www\project\classes\class_main.php on line 22

I have tried:

  • using require_once as a function
  • using dirname(__FILE__) and __DIR__
  • using \ and \\ instead of /

How can I do this?

The full code of the __destruct function at the moment is:

    function __destruct()
    {
        if($this->admtrim($_SERVER['PHP_SELF']) == 'admin')
        {
            require_once "../templates/foot.php";
        }
        else
        {
            require_once './templates/foot.php';
        }
    }

To clarify, this script is located at C:\wamp\www\project\classes\class_main.php and the file I am trying to include is located at C:\wamp\www\project\templates\foot.php.

Seabody
  • 1,197
  • 1
  • 12
  • 27

2 Answers2

0
C:\wamp\www\project\classes/templates/foot.php

This should be self explanatory, look at back and forward slashes both in one link. This should be

C:\wamp\www\project\classes\templates\foot.php

Try with backslashes in windows Format

EDIT:

As you mentioned the file is located at C:\wamp\www\project\templates\foot.php Then why not provide the complete path

require_once("C:\wamp\www\project\templates\foot.php");
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
  • I have tried that - `require_once "..\\templates\\foot.php";` and `require_once "..\templates\foot.php";` both do not work. – Seabody Mar 05 '13 at 06:53
  • `C:\wamp\www\project\classes\templates\foot.php` Does this file even exist at that very location? – Hanky Panky Mar 05 '13 at 06:55
  • 1
    Instead of messing with `\ ` and `/`, use `DIRECTORY_SEPARATOR`. That's always correct for the system you're using. – Arjan Mar 05 '13 at 06:59
  • 1
    No, the file exists at `C:\wamp\www\project\templates\foot.php` - no matter how I require it, the file is never included, I always get an error like the one I posted above, or `require_once(./templates/foot.php) [function.require-once]: failed to open stream: No such file or directory in C:\wamp\www\project\classes\class_main.php on line 22` – Seabody Mar 05 '13 at 07:00
  • Ok, can you please post the code which is including this file? Also look at Edwin's answer below which is correct that your path is looking for file elsewhere. But you have to post some code to get a quicker answer – Hanky Panky Mar 05 '13 at 07:02
0

Your templates folder is located at outside of the classes folder. But the error shows, it searches inside classes folder.

C:\wamp\www\project\classes/templates/foot.php

Include it correctly.

Edwin Alex
  • 5,118
  • 4
  • 28
  • 50
  • How? Am I missing a period at the beginning? I've checked, I'm sure I'm including it correctly. – Seabody Mar 05 '13 at 07:20