Web Hosting Talk







View Full Version : include


mattschinkel
05-07-2003, 08:38 PM
how do I check to see if an include has failed without echoing an error on the page.

eg, if include file.php failed, then $var = "failed";

plugged
05-07-2003, 10:12 PM
<?php
$status = 0;
include("file.php"); // file should contain a line that says $status = 1

if ($status = 0)
echo "include failed!";
else
echo "include worked!";

die();
?>

digitok
05-07-2003, 11:10 PM
<?
@include('file.php') or die('Failed to open file.php');
?>

The actual include() won't error because of the @, and if it fails to open it will echo 'Failed to open file.php' but you can add whatever in there.

You could also do...


<?
$include = @include('file.php');
if (!$include) {
// Do whatever here because the file didn't open
}
?>

Hope it helps.

CSD_Hosting
05-07-2003, 11:51 PM
just include a var inside the included file like $require

then in your main file do:


<?php
// file main.php
include("include.php");

if( !$require )
{
echo "cannot include file.";
}
?>


for the include


<?
// file include.php
$require = 1;
?>

X-Istence
05-08-2003, 03:41 AM
the thing that digitok posted is the most elegant and fastest.

mattschinkel
05-08-2003, 09:27 AM
Thanks!
I just wanted to make a php cron job to check if apache is running ok, if not, apache gets reset and I get emailed.

works good!
I used digitok's second piece of code..

MDJ2000
05-08-2003, 10:31 AM
just for reference, digitok's code is the only one that will work correctly. The first example given doesn't even use a correct comparision operator, so it will always return "Include Worked!", but only after it prints the error...

CSD_Hosting
05-08-2003, 05:55 PM
ah ok

digitok
05-10-2003, 09:24 AM
Glad to be of help matt :)