Jump to content

[SOLVED] Find files relative to included script


svivian

Recommended Posts

Is it possible to find the path to a file relative to an included script?

 

For example I have one file, test.php, that includes another in subfolder/config.php. The config.php file stores a relative path to a file I want to read, eg $file = 'data/file.txt';

 

If I open config.php the file is read without trouble, but if I open test.php the file can't be found. Is there a way to make sure I always find the file, regardless of which files include config.php?

Link to comment
Share on other sites

Forgot to mention that. It won't work in all cases since $_SERVER['DOCUMENT_ROOT'] gives the same value on a domain and subdomain, e.g. www.example.com and site.example.com - but the real document root for each site is different.

 

This is for a script I'm writing to release to the public. So in addition, the subfolder could be anywhere in the site structure (the text file will always be in the same place relative to the subfolder); I need to be able to detect the folder automatically.

Link to comment
Share on other sites

Then why not do it this way?

 

function getDirectory( $path , $file_to_find){

    $dh = @opendir( $path );
    // Open the directory to the handle $dh
    
    while( false !== ( $file = readdir( $dh ) ) ){
    // Loop through the directory (this checks to see if the next file exists, otherwise it's the end of the directory)
    
            if($file_to_find == $file){
            return $path;
            }
     
            if( is_dir( "$path/$file" ) ){
            // Its a directory, so we need to keep reading down...
            
                echo "<strong>$spaces $file</strong><br />";
                getDirectory( "$path/$file" , $file_to_find);
                // Re-call this same function but on a new directory.
                // this is what makes function recursive.
            
            }
            
            }
        
        }
    
    }
    
    closedir( $dh );
    // Close the directory handle

}

?>

 

Then run getDirectory("root folder","file to find")

Link to comment
Share on other sites

What you would do is this:

 

Have two files:

1.php -- it will include 2.php, which is located in a different folder

2.php -- will print environmental array, server array...

 

So, you load 1.php in your browser and see if you get a variable with the info you're looking for.

Link to comment
Share on other sites

Ah, nevermind I've found a better solution: __FILE__

This always returns the name of the file it's in. For reference, here's how I get the folder:

 

<?php
$included_file = str_replace( '\\', '/', __FILE__ ); // to fix Windows 
$included_directory = substr( $included_file, 0, strrpos( $included_file, '/' ) );
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.