1

我正在尝试查找服务器的 public_html 文件夹中是否存在文件。

第一次尝试:

if ( file_exists('./local-config.php')) {

但这仅在调用上述 if 语句的 php 文件位于同一文件夹中时才有效……不好……

第二次尝试

if ( file_exists( $_SERVER['SERVER_NAME'].'/local-config.php' ) ) {

不工作...

第三次尝试

if ( file_exists( 'http://'. $_SERVER['SERVER_NAME'].'/local-config.php' ) ) {

也不行……

第四次尝试

$_SERVER['DOCUMENT_ROOT'] seems to fall short of my public_html folder!

有任何想法吗?

如果我输入完整路径,我可以让它工作......例如

if ( file_exists( '/home/username/public_html/local-config.php' ) ) {

但这对我没有好处,因为该脚本可以在多个服务器上使用,所以我需要一些通用的东西。

是否可以让 PHP 动态返回 public_html 路径?

4

2 回答 2

1

为此,我在我的应用程序中创建了一个全局变量。如同:

define('ROOT', dirname(__FILE__));

然后我将其与文件名一起使用,例如:

ROOT . '/my_file.php'
于 2013-02-04T19:28:25.500 回答
0

尝试做这样的事情:

$path = dirname(__FILE__);
// or just __DIR__ - it should be the same thing

这将返回被调用文件的完整路径。 __FILE__是 PHP 中的一个“魔法常数”。从帮助页面:

The full path and filename of the file. If used inside an include, the name of
the included file is returned. Since PHP 4.0.2, __FILE__ always contains an
absolute path with symlinks resolved whereas in older versions it contained
relative path under some circumstances. 

通过生成的路径,您应该能够弄清楚如何获得您想要的。

于 2013-02-04T19:06:47.913 回答