"include file" check

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
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

"include file" check

Post by AGISB »

I am creating a help system. I call help.php?id=15 and in help.php a language file with the text for page 15 is included. If there is no file it should display the 'no help available text'.

If I do something like

Code: Select all

if (include 'file.php') {

}
it gives me the usual warning that the include is not there. Is there a way to check if the file exists without any warning messages?
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Code: Select all

if(file_exists($file)){
  include($file);
}
else {
  echo "There is no file for that ID.";
}
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post by AGISB »

file_exists() does not work. It returns false although the file is there.

php.net docs comments have some of this behaviour described. As this can happen with different setups I cannot use it to determine the existance.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

you could potentially supress the error..

Code: Select all

if (@include($file))
{
  //yup
}
else
{
  //nope
}
Although that may also supress any parse errors or alike.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

could you not then do something like

Code: Select all

if( fopen($file.php, "r"))
  include($file.php)
else
  echo "some error message";
User avatar
cheatboy00
Forum Contributor
Posts: 151
Joined: Sat Jun 29, 2002 10:36 am
Location: canada
Contact:

Post by cheatboy00 »

um

Code: Select all

if (is_file($fileName)) {
   // Do something crazy
} else {
   // I'm sad
}
should work
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

Well on my multi-lingual website, I have a form select on every page to change the language. And I include this file at the top of every page, it determines the language two letter code to use. And after it, I use include with the language code to include the right language file. Might give you some ideas. If you want to talk more about internationalization of websites with me, you can priv msg me ok.

Code: Select all

<?php
// Very Uppermost Code for Every php Page
// Only PHP Code Allowed Here!

$tlcode = $_COOKIE['browselang'];

// Did they execute the change lang dropdown?
if ($_POST['selectlang']) {
    setcookie("browselang", $_POST['selectlang'], (time() + (3600 * 24 * 365 * 10)), "/");
    $tlcode = $_POST['selectlang'];
}

// Get the two letter code from the browser prefered language
$prefer_tlc = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2);

// Get the main language file and apex (charset codes)
// If the cookie isn't there, try HTTP_ACCEPT_LANGUAGE
// if we support it, else default to english.
if (empty($tlcode)) {
  if (file_exists("language/".$prefer_tlc)) {
    // If /cms/language/<tlc_here>/ exists, set it to that.
    setcookie("browselang", $prefer_tlc, (time() + (3600 * 24 * 365 * 10)), "/");
    include("language/".$prefer_tlc."/apex-".$prefer_tlc.".".$phpEx);
    include("language/".$prefer_tlc."/cpsnav-".$prefer_tlc.".".$phpEx);
	$tlcode = $prefer_tlc;
  } else {
    // Set it to English (Default)
    setcookie("browselang", "en", (time() + (3600 * 24 * 365 * 10)), "/");
    include("language/en/apex-en." . $phpEx);
    include("language/en/cpsnav-en." . $phpEx);
	$tlcode = "en";
  }
} else {
	include("language/".$tlcode."/apex-".$tlcode."." . $phpEx);
	include("language/".$tlcode."/cpsnav-".$tlcode."." . $phpEx);
}

// Turn off Warnings
error_reporting(E_ERROR | E_PARSE);

// We have to send this header at the top of every php page
// This defines the language used on the page
header('Content-Type: text/html; charset=' . $charset);

?>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Sounds like you're making the same mistake I always make....

Not pointing to the full path for the include file... (I have a nasty habit of just giving the file name and pulling my hair out every time. Now I go shoot myself in the foot...) :lol:
Post Reply