-1

I have a question...I am running a php page daily through a crobtab and I seem to be getting erros when this code is run..

include(CLASS_PATH .'EmailAddressValidator.php');

so I applied this code...

if(file_exists('/var/www/testsite/classes/EmailAddressValidator.php')){
    include('/var/www/testsite/classes/EmailAddressValidator.php');
}else{
    include(CLASS_PATH .'EmailAddressValidator.php');
}

but I also have live site at

/var/www/livesite/classes/EmailAddressValidator.php

and its been a pain doing back and forth changing my code fromtestsite to livesite is there an easier way to do this..

I've tried..

if(file_exists('/var/www/testsite/classes/EmailAddressValidator.php')){
    include('/var/www/testsite/classes/EmailAddressValidator.php');
elseif(file_exists('/var/www/livesite/classes/EmailAddressValidator.php')){
    include('/var/www/livesite/classes/EmailAddressValidator.php');
}else{
    include(CLASS_PATH .'EmailAddressValidator.php');
}

Testing out all answers...will have a vote by monday end day

user979331
  • 11,039
  • 73
  • 223
  • 418

5 Answers5

2

You can use relative paths instead

../classes/EmailAddressValidator.php

or whatever the path is.

Mattias
  • 9,211
  • 3
  • 42
  • 42
  • okay why?, it works for me... do you mean it may should be `./classes/EmailAddressValidator.php` or something else? I do not now in which file you includes from. – Mattias Apr 30 '12 at 17:21
2

You need to make sure you're using consistently defined directory locations, especially if you're frequently changing between production and development environments. If you also work on different operating systems, getting the directory structures right can be a real pain in the neck, so if you centralise things it makes your life a lot easier.

For example, are you sure you're defining CLASS_PATH as a constant?

I'd suggest finding the location of the current directory before you do anything else:

$current_dir = dirname(__FILE__);

and then using this variable to go onwards and upwards:

$test_dir = $current_dir . '../testsite/';
$live_dir = $current_dir . '../livesite/';
$class_dir = $current_dir . '../classes/';

if(file_exists("$test_dir/EmailAddressValidator.php"))
{
    include("$test_dir/EmailAddressValidator.php");
}
elseif(file_exists("$live_dir/EmailAddressValidator.php"))
{
    include("$live_dir/EmailAddressValidator.php");
}
else
{
    include("$class_dir/EmailAddressValidator.php");
}
hohner
  • 11,498
  • 8
  • 49
  • 84
1

1.) you could use a configuration file or environment variable to setup your "environment":

environment.ini

environment = livesite

cronscript.php:

$config      = parse_ini_file("./environment.ini");
$environment = $config["environment"];

include "/var/www/$environment/classes/EmailAddressValidator.php";

Now add the environment.ini to your livesite and testsite, each with it's corresponding entry in the configuration file.

Of course you can use other configuration types, such as constants (define("MY_ENVIRONMENT", "livesite").

2.) Another approach would be to use relative file paths:

include "../relative/path/to/classes/EmailAddressValidator.php"

3.) Use an autoloader

MonkeyMonkey
  • 826
  • 1
  • 6
  • 19
1

Well you could use the $_SERVER['SCRIPT_FILENAME'] variable to manipulate the if else in runtime.

I mean you could get whether you are using testsite or livesite by doing some substr or explode on the variable and pass that with the path in the if else statements

Sandeep Rajoria
  • 1,243
  • 8
  • 14
0

Normally you would do the following

include dirname(__FILE__) . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . 'EmailAddressValidator.php';

This would automatically find the path of the currently running file and include the file you want based on its position in relation to the running file.

Using the DIRECTORY_SEPARATOR constant will ensure your code will work on either windows or linux systems

Anigel
  • 3,435
  • 1
  • 17
  • 23