PHP Check if any file is under certain directory |
For a list of all requirements and guidelines pertaining to posting a new Help topic, please click here.
This Month's Contests | Staff Member of the Month | Hosts Looking for Hostees | Hostees looking for Hosts | BigBookofResources
Submission Guidelines
PHP Check if any file is under certain directory |
Sep 27 2010, 10:07 PM
Post
#1
|
|
Mel Blanc was allergic to carrots. Group: Official Designer Posts: 6,371 Joined: Aug 2008 Member No: 676,291 |
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. |
|
|
Sep 28 2010, 12:14 PM
Post
#2
|
|
Member Group: Member Posts: 22 Joined: Dec 2008 Member No: 702,109 |
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"; ?> |
|
|
Sep 28 2010, 04:07 PM
Post
#3
|
|
Mel Blanc was allergic to carrots. Group: Official Designer Posts: 6,371 Joined: Aug 2008 Member No: 676,291 |
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/
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. |
|
|
Sep 28 2010, 04:53 PM
Post
#4
|
|
Senior Member Group: Administrator Posts: 2,648 Joined: Apr 2008 Member No: 639,265 |
Use glob:
CODE if (in_array($uri, glob('/path/to/*.php'))) {
echo 'Page active'; } else { echo 'No page active'; } |
|
|
Sep 30 2010, 10:40 PM
Post
#5
|
|
Mel Blanc was allergic to carrots. Group: Official Designer Posts: 6,371 Joined: Aug 2008 Member No: 676,291 |
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.'; |
|
|
Oct 1 2010, 09:11 AM
Post
#6
|
|
Senior Member Group: Administrator Posts: 2,648 Joined: Apr 2008 Member No: 639,265 |
Use ** to match directories and subdirectories.
|
|
|