Reputation: 421
I have an include statement in my program:
include_once("C:/apache2.2/htdocs/AdminTool/includes/functions.php");
//include_once("/var/www/AdminTool/includes/functions.php");
I only use one at a time. In the code above I am using it for my localhost. But if I will run it on server, I have to comment the local one. Because it will cause error.
Is there a way to run them both and if it does not find the file no error is returned. Because it is tiring to change them back and forth and I have many .php files like this.
Kind of like (not an actual code):
if (include_once("C:/apache2.2/htdocs/AdminTool/includes/functions.php");)
else (include_once("/var/www/AdminTool/includes/functions.php");)
Upvotes: 2
Views: 36
Reputation: 695
file_exists: http://php.net/manual/en/function.file-exists.php
is_dir: http://php.net/manual/en/function.is-dir.php
Best way to do that is use dirname(__FILE__)
which gets the directory's full path of the current file in ether unix of windows format. Then we use realpath()
which conveniently returns false if file does not exist. All you have to do is specify a relative path from that file's directory and put it all together:
$path = dirname(__FILE__) . '/include.php';
if (realpath($path)) {
include($path);
}
Upvotes: 1