file_exists($file);
does the trick for both relative and absolute paths.
What's more useful, however, is having absolute paths without hardcoding it. The best way to do that is use dirname(__FILE__)
which gets the directory's full path of the current file in ether UNIX or Windows format. Then we can use realpath()
which conveniently returns false if file does not exist. All you have to do then 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);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…