Forum Moderators: coopster

Message Too Old, No Replies

PHP, include, if file not exist?

I'm using the statment "include_once", if the file does not exist...

         

patrixs

7:16 pm on Mar 13, 2003 (gmt 0)

10+ Year Member



Hy, I'm using the statment "include_once", if the file does not exist how do I put an error message instead and make the "WARNING" not appear?

I currently have :

// The page to include
$page = include_once $path . "pages/$page[ask].php";
// If page does not exist
if(!$page) { print "error"; }
// If page does exist
else { print "good"; }

The "error" and "good" message work but I still have this message above : "Warning: Failed opening ...", how do I remove that message?

Thanx

aaronc

7:20 pm on Mar 13, 2003 (gmt 0)

10+ Year Member



I don't think that's possible.
You'll probably have to use file_exists first and then if the file exists then include the file.

jatar_k

7:23 pm on Mar 13, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if (is_file [php.net]($somefile)) {
include stuff;
} else {
dont include stuff;
}

RichD

7:25 pm on Mar 13, 2003 (gmt 0)

10+ Year Member



Just change
$page = include_once $path . "pages/$page[ask].php";

to
$page = @include_once $path . "pages/$page[ask].php";

that should cause the warning to be suppresed.

lorax

7:26 pm on Mar 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Why not test for the existence of the file first and then execute the include?

nosanity

8:23 pm on Mar 13, 2003 (gmt 0)

10+ Year Member



Also, if you really need the file, test to see if it exists. If it doesn't, display a nice friendly error page. If a file is not included when it needs to be, and you surpress the warning, it can lead to functions not working, variables missing, and lots of other really cool errors that can be a real pain if you forget about the supression.

noSanity

patrixs

8:42 pm on Mar 13, 2003 (gmt 0)

10+ Year Member



lots of other really cool errors that can be a real pain if you forget about the supression

LOL!

Ok, thanxs all you guys! I'll try that!