Jump to content

[SOLVED] file_exists only works in root dir


cganim8

Recommended Posts

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*/

?>

 

 

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

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()?

$_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" />'; }

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.