Jump to content

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

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.