Search code examples
phplinuxfile-exists

file_exists - PHP return dynamic public_html path


I am trying to find if a file exists in the public_html folder of the server.

1st try:

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

but this only works if the php file calling the above if statement is in the same folder... no good...

2nd try

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

doesn't work...

3rd try

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

also doesn't work...

4th try

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

Any ideas?

I can get this working if I put in the full path... e.g.

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

however this is no good to me because the script may be used on more than one server, so I need something generic.

Is it possible to get PHP to dynamically return the public_html path?


Solution

  • I create a global variable across my application for this purpose. Similar to:

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

    Then I use this with the filename like:

    ROOT . '/my_file.php'