Printable Version of Topic

Click here to view this topic in its original format

Forums _ Webmasters' Corner _ PHP Check if any file is under certain directory

Posted by: Mikeplyts Sep 27 2010, 10:07 PM

Thinking it'd a be a lot more efficient, I was wondering: how do you check to see if a file is under a certain directory? Basically, it would be something similar to a wildcard or something.

I'm pretty familiar with checking the URL's file name (in the most basic method):

CODE
<?php
$uri = $_SERVER['SCRIPT_NAME'];

if ($uri == '/path/to/file.php') echo 'Woo.'; else echo 'Aw.';
?>


Now, what I'm imagining, when it comes to checking for any file under a directory, is this:

CODE
<?php
$uri = $_SERVER['SCRIPT_NAME'];
$name = *WILDCARD SORT OF SHIT HERE*;

if ($uri == '/path/to/' . $name . '.php') echo 'Woo.'; else echo 'Aw.';
?>


Any ideas would be awesomely appreciated. Yeah, awesomely is a word.

Posted by: ahmad Sep 28 2010, 12:14 PM

This should work.

CODE
<?php
// file_exists: http://php.net/manual/en/function.file-exists.php
    $file = "/path/to/file.ext";

    if( file_exists( $file ) )
        echo "File Found\n";
    else
        echo "File Found\n";
    
?>

Posted by: Mikeplyts Sep 28 2010, 04:07 PM

Ergh, no. That's not really what I was looking for. By the way, this sort of for "marking" purposes.

I may have to rephrase what I meant -- I'm trying to check the directory. Let's say I have some files under the directory /path/to/. Then, let's say I have a function that displays a message of which page is currently "active."

/path/to/

Instead of checking to see if each file exists under that directory:

CODE
<?php
$uri = $_SERVER['SCRIPT_NAME'];
$file = array(
    'one' => '/path/to/one.php',
    'two' => '/path/to/two.php',
    'three' => '/path/to/three.php',
    'four' => '/path/to/four.php',
    'five' => '/path/to/five.php'
);

if ($uri == $file['one']) echo 'Page is active.';
else if ($uri == $file['two']) echo 'Page is active.';
else if ($uri == $file['three']) echo 'Page is active.';
else if ($uri == $file['four']) echo 'Page is active.';
else if ($uri == $file['five']) echo 'Page is active.';
else echo 'No page is active.';
?>


It'd be more efficient, I would think, to just use something along the lines of a "wildcard" (my proposed concept) to check if there is an active file that is under that certain directory, no matter the file name, as long as the file is active and under that directory :

CODE
<?php
$uri = $_SERVER['SCRIPT_NAME'];
$all = *WILDCARD METHOD HERE*; // My "wildcard" variable that would have all the file names under a directory.

if ($uri == '/path/to/' . $all . '.php') echo 'Page is active.';
else echo 'No page is active.';
?>


Then, I'd be able to use that method in a function of some sort to use in a header file as opposed to individually marking each file with that text whereas PHP could just do it for me.

Oh, and while writing this, I got the idea that I had to figure out how to get all the file names under the directory and store them somehow into a variable or something where the variable would represent the wildcard, in this case.

Posted by: mipadi Sep 28 2010, 04:53 PM

Use http://php.net/manual/en/function.glob.php:

CODE
if (in_array($uri, glob('/path/to/*.php'))) {
    echo 'Page active';
} else {
    echo 'No page active';
}

Posted by: Mikeplyts Sep 30 2010, 10:40 PM

Hrm, that doesn't seem to work.

Oh, what if I also wanted to check if there were directories under certain directories? And let's say I specifically want to find a certain file that is common throughout those directories under a directory. I'd think it's something like this, but it doesn't seem to work either:

CODE
if (in_array($uri, glob('/path/*/file.php'))) echo 'Directory active.';
else echo 'No directory active.';

Posted by: mipadi Oct 1 2010, 09:11 AM

Use ** to match directories and subdirectories.