Jay wrote:
Hi!

Is there a ready to use PHP function for including ONLY if the file exists?

I tried it like this:

[main.php]
include_once("lib.php");
....later
includeIf("somefile.php");

[lib.php]
function includeIf($filename)
{
    if (!file_exists($filename))
    {
        include_once($filename);
    }
}

You see the problem. The somefile.php seems to be included in the wrong file. It should be included in the main.php instead of lib.php.

How can i still have the function in lib.php but the file will be included in main.php

Don't see why you need an 'includeif' function at all. include() will not stop the script (fatal error) if the file isn't there like require() will. include() will only throw an error which you can supress with '@'


@include ( "nonexistant.txt" )

include() will also return true or false. So if you want to run some code only if the include file is there...

if ( @include ( "thisfile.txt" ) ) {
        //  do this
        some code;
}

Your function is working fine, what's confusing you is scope. When you include a file inside of a function, everything in that file has the scope of the local function, not the document itself.

--
By-Tor.com
It's all about the Rush
http://www.by-tor.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to