Page 1 of 1

"include file" check

Posted: Sat May 14, 2005 1:17 pm
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?

Posted: Sat May 14, 2005 1:20 pm
by d3ad1ysp0rk

Code: Select all

if(file_exists($file)){
  include($file);
}
else {
  echo "There is no file for that ID.";
}

Posted: Sat May 14, 2005 1:43 pm
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.

Posted: Sat May 14, 2005 2:04 pm
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.

Posted: Sun May 15, 2005 3:49 am
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";

Posted: Sun May 15, 2005 7:20 am
by cheatboy00
um

Code: Select all

if (is_file($fileName)) {
   // Do something crazy
} else {
   // I'm sad
}
should work

Posted: Sun May 15, 2005 10:32 am
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);

?>

Posted: Mon May 16, 2005 5:41 am
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: