Forum Moderators: coopster

Message Too Old, No Replies

include() within a function

Can't get anything to work

         

migthegreek

12:16 pm on Jul 1, 2009 (gmt 0)

10+ Year Member



I am calling a function which includes a file, but the include can't be found. Example:

// functions.php


function customInclude() {
include($_SERVER['DOCUMENT_ROOT'] . '/myfile.php');
}

I get this warning:

Warning: include(/var/www/path/root/myfile.php) [function.include]: failed to open stream: No such file or directory in /var/www/path/root/includes/functions.php on line 140

...but that is the correct path to the file, so why is it saying it can't find it?

P.S. Why does the BB code formatting suck so much here?

jatar_k

1:14 pm on Jul 1, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well if it can't find it then the path isn't correct as far as that script is concerned. Most often this problem arises when people forget that it is a path from the including file, not the included but you have it absolute.

you could try echo'ing the path in that file and see what you get

you could try the relative include to see if that works, just for the sake of it

migthegreek

1:21 pm on Jul 1, 2009 (gmt 0)

10+ Year Member



The idea is that I don't know where the path is actually relative to. I want a function that can blindly include any file, via an absolute URL.

I have tried echoing the path, and it is correct. I don't understand why it can't be found.

Are there any limitations with including within a function? Is it a scope issue?

jatar_k

1:31 pm on Jul 1, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



it shouldn't be scope, the filesystem is not out of scope

the thing is the file isn't where that path says, hence the error

I just meant try once relative to see if it works, that would answer some of your questions. Try including from the same dir using no path or something.