svivian Posted October 13, 2007 Share Posted October 13, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/73128-solved-find-files-relative-to-included-script/ Share on other sites More sharing options...
kratsg Posted October 13, 2007 Share Posted October 13, 2007 Try an absolute server path. include($_SERVER['DOCUMENT_ROOT'].'/filename.ext'); That should work :-) Quote Link to comment https://forums.phpfreaks.com/topic/73128-solved-find-files-relative-to-included-script/#findComment-368767 Share on other sites More sharing options...
svivian Posted October 13, 2007 Author Share Posted October 13, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/73128-solved-find-files-relative-to-included-script/#findComment-368794 Share on other sites More sharing options...
kratsg Posted October 13, 2007 Share Posted October 13, 2007 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") Quote Link to comment https://forums.phpfreaks.com/topic/73128-solved-find-files-relative-to-included-script/#findComment-368804 Share on other sites More sharing options...
pkSML Posted October 13, 2007 Share Posted October 13, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/73128-solved-find-files-relative-to-included-script/#findComment-368806 Share on other sites More sharing options...
svivian Posted October 13, 2007 Author Share Posted October 13, 2007 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, '/' ) ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/73128-solved-find-files-relative-to-included-script/#findComment-368823 Share on other sites More sharing options...
kratsg Posted October 13, 2007 Share Posted October 13, 2007 Oh, I see what you mean. You're just gonna take the file that includes a file, take the include url, add the file's url, and send that off? Quote Link to comment https://forums.phpfreaks.com/topic/73128-solved-find-files-relative-to-included-script/#findComment-368825 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.