PDA

View Full Version : How to do an include only if file exists?


Moonlight
03-17-2000, 02:47 PM
I'm trying to do some includes depending of a value of the field in the database.

Let say I have a variable saying $A_Game = "eq";

I currently have an include statement that does like :
include ("incl_char_$A_Game.php");[nbsp][nbsp]// Would include incl_char_eq.php

The include file is to ask other questions depending on the selection of $A_Game.

Now, not every $A_Game value would have to ask other questions.

I would like to be able to only do the include IF there the include file actually does exist. That would mean there is no extra questions to ask. The included file is not on the www accessible path.

How do I do it?

Moonlight

SneakyDave
03-17-2000, 03:27 PM
$theresafile = @file_exists('/big/dom/xdomain/foo.bar');
IF($theresafile):
[nbsp][nbsp][nbsp][nbsp]include '/big/dom/xdomain/foo.bar';
ENDIF;


INCLUDE might work better than REQUIRE because I think REQUIRE verifies the file exists in the PHP pre-processor, of sorts.

Does that help?

[edited for error]
[This message has been edited by SneakyDave (edited 03-17-00@2:29 pm)]

SneakyDave
03-17-2000, 04:05 PM
Errr.... that code won't quite work right, this is the correct version:
<?
$theresafile = @file_exists('/big/dom/xdomain/foo.bar');
IF($theresafile):
[nbsp][nbsp][nbsp][nbsp]include '/xdomain/www/foo.bar';
ENDIF;
?>

Replacing include with require will NOT work, as require looks for the file before parsing of code even begins.

Also, the goofiness of PHP here requires a slightly different directory location to be used for file_exists and the include statement.

--Tested it this time!


[This message has been edited by SneakyDave (edited 03-17-00@3:06 pm)]