include error trapping - help!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
crabyars
Forum Commoner
Posts: 37
Joined: Thu Jun 17, 2004 8:24 pm

include error trapping - help!

Post by crabyars »

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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

try using [php_man]fopen[/php_man] if you want to search the include path.
crabyars
Forum Commoner
Posts: 37
Joined: Thu Jun 17, 2004 8:24 pm

Post by crabyars »

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:

Code: Select all

$includefile = "foo.php";
if ( !@include($includefile) ) {
    echo "ERROR: $includefile not found in include path!";
}
This seems to work fine, handy for simple error feedback.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

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:

Code: Select all

$includefile = "foo.php";
if ( !@include($includefile) ) {
    echo "ERROR: $includefile not found in include path!";
}
This seems to work fine, handy for simple error feedback.
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).

Example....
include.php

Code: Select all

<?php
if (!@include('fatal_error.php'))
{
    echo 'Failed to include fatal_error.php';
}
?>
fatal_error.php

Code: Select all

<?php
trigger_error('I''m a fatal error', E_USER_ERROR);
?>
Running fatal_error.php on it's own produces a fatal error (no surprise there). However, running include.php results in a blank page! (this is the case with PHP4.3.7 not sure about others)
crabyars
Forum Commoner
Posts: 37
Joined: Thu Jun 17, 2004 8:24 pm

Post by crabyars »

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..
crabyars
Forum Commoner
Posts: 37
Joined: Thu Jun 17, 2004 8:24 pm

Post by crabyars »

Ok, this seems to work as well, from Feyd's initial suggestion

Code: Select all

$includefile="foo.php";
$handle = fopen($includefile, "r", 1);
if ($handle) &#123;
	fclose($handle);
	include ($includefile);
&#125; else &#123;
	echo "file: $includefile not found in path"; 
	echo "handle is: $handle";
&#125;
any comments?
Post Reply