cganim8 Posted December 18, 2008 Share Posted December 18, 2008 I am using PHP Version 5.2.6 on a test server running Apache/2.2.9 (Win32) I have safe_mode Off I have been trying to use glob() and file_exists() and I can't get them to work when the script is called from a sub directory. What am I doing wrong? <?php print_r(glob("http://www.sitename.com/*.php")); /* returns Empty Array */ echo "<br />"; print_r(glob("/*.php")); /* returns Empty Array */ echo "<br />"; print_r(glob("*.php")); /* THIS WORKS Only if the script is in the root dir*/ echo "<br />"; if (file_exists("Images/tab.jpg")) { echo "<br /> TRUE";} else {echo "<br /> FALSE";} /* returns TRUE if the script is in the root dir*/ if (file_exists("/Images/tab.jpg")) { echo "<br /> TRUE";} else {echo "<br /> FALSE";} /* returns FALSE */ if (file_exists("/index.html")) { echo "<br /> TRUE";} else {echo "<br /> FALSE";} /* returns FALSE */ if (file_exists("index.html")) { echo "<br /> TRUE";} else {echo "<br /> FALSE";} /* returns TRUE if the script is in the root dir*/ ?> Quote Link to comment https://forums.phpfreaks.com/topic/137497-solved-file_exists-only-works-in-root-dir/ Share on other sites More sharing options...
xtopolis Posted December 18, 2008 Share Posted December 18, 2008 A little confusing to read, post your file structure? [pre] ./ glob.php +-- images [/pre] glob("./*.php");//grab php files in current dir glob("../*.php");//grab php files from parent directory(in this case, a directory above ./ , access permitting) glob("./images/*.php");//grab php files from sub directory Quote Link to comment https://forums.phpfreaks.com/topic/137497-solved-file_exists-only-works-in-root-dir/#findComment-718561 Share on other sites More sharing options...
Mark Baker Posted December 18, 2008 Share Posted December 18, 2008 And don't forget: glob("/*.php");//grab php files from the server's root directory Note, the server's root directory, not the htdocs root Quote Link to comment https://forums.phpfreaks.com/topic/137497-solved-file_exists-only-works-in-root-dir/#findComment-718575 Share on other sites More sharing options...
cganim8 Posted December 18, 2008 Author Share Posted December 18, 2008 And don't forget: glob("/*.php");//grab php files from the server's root directory Note, the server's root directory, not the htdocs root OK, I think I see the error in my logic. "/*.php" takes me to the SERVERS root NOT the WEBSITE root directory. So, you are saying that I need to do something like: if (file_exists("../../../images/image.jpg") { <image src="/images/image.jpg" /> } Correct? Is there an easier way to get to the root of the Website directory if my script is buried deep in the website directory structure using glob() or file_exists()? Quote Link to comment https://forums.phpfreaks.com/topic/137497-solved-file_exists-only-works-in-root-dir/#findComment-718973 Share on other sites More sharing options...
wildteen88 Posted December 18, 2008 Share Posted December 18, 2008 $_SERVER['DOCUMENT_ROOT'] holds the absolute path to your sites root folder, eg /home/yoursite.com/htdocs/ define('BASE', $_SERVER['DOCUMENT_ROOT']); if (file_exists(BASE."/images/image.jpg") { echo '<image src="/images/image.jpg" />'; } Quote Link to comment https://forums.phpfreaks.com/topic/137497-solved-file_exists-only-works-in-root-dir/#findComment-719010 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.