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*/ ?> 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 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 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()? 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" />'; } 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
Archived
This topic is now archived and is closed to further replies.