I'm working on a small script, it needs to include a file (which should already be in the include path)
I would like to trap include errors, so that if the file can't be found I can display a message like "cannot find xxx.php"
I tried file_exists, but that doesn't seem to search through the include path.
thanks!
include error trapping - help!
Moderator: General Moderators
Thanks feyd,
that proved to be a wee bit tricky, I've found a clean solution (I think) and so I'll post it here:
This seems to work fine, handy for simple error feedback.
that proved to be a wee bit tricky, I've found a clean solution (I think) and so I'll post it here:
Code: Select all
$includefile = "foo.php";
if ( !@include($includefile) ) {
echo "ERROR: $includefile not found in include path!";
}This method doesn't actually work too well! The @ infront of the include not only supresses errors generated by the actual include function but it also supresses errors within the included file (even fatal errors which surprised me).crabyars wrote:Thanks feyd,
that proved to be a wee bit tricky, I've found a clean solution (I think) and so I'll post it here:
This seems to work fine, handy for simple error feedback.Code: Select all
$includefile = "foo.php"; if ( !@include($includefile) ) { echo "ERROR: $includefile not found in include path!"; }
Example....
include.php
Code: Select all
<?php
if (!@include('fatal_error.php'))
{
echo 'Failed to include fatal_error.php';
}
?>Code: Select all
<?php
trigger_error('I''m a fatal error', E_USER_ERROR);
?>Hey, thanks for the note. I want to try and get it right.
I tried your examples, but I don't get any error messages when I run fatal_error.php alone. I'm on PHP Version 4.3.4, perhaps my error reporting is at a diff level than yours.. hmmm
Do you have any other suggestions to do what I'm trying to accomplish? Perhaps I need to go Feyd's route after all..
I tried your examples, but I don't get any error messages when I run fatal_error.php alone. I'm on PHP Version 4.3.4, perhaps my error reporting is at a diff level than yours.. hmmm
Do you have any other suggestions to do what I'm trying to accomplish? Perhaps I need to go Feyd's route after all..
Ok, this seems to work as well, from Feyd's initial suggestion
any comments?
Code: Select all
$includefile="foo.php";
$handle = fopen($includefile, "r", 1);
if ($handle) {
fclose($handle);
include ($includefile);
} else {
echo "file: $includefile not found in path";
echo "handle is: $handle";
}