directory exist problem on different php versions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
MindOverBody
Forum Commoner
Posts: 96
Joined: Fri Aug 06, 2010 9:01 pm
Location: Osijek, Croatia

directory exist problem on different php versions

Post by MindOverBody »

I am using function below, and it working well on my localhost wamp server with PHP 5.3, but
it is not working on PHP 5.2. Do anybody know a reason why it is returning false even if directory exist?

Code: Select all

       function DirExists( $FullPath, $Dirname ) {
            $found = null;
            if ( ( $directoryHandle = opendir( $FullPath ) ) == true ) {
                while ( ( $file = readdir( $directoryHandle ) ) !== false ) {
                    // Make sure not delaing with file or parent links (., ..)
                    if ( is_dir( $FullPath . $file ) && ( $file == '.' || $file == '..' ) !== true ){
                       if ( $Dirname == $file ){
                            $found = 1;
                       } else $found = 0;
                    }     
                }
            }
            return $found == 1 ? true:false;
        }
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: directory exist problem on different php versions

Post by requinix »

Ugh.

Okay. Let's say the parent directory has the contents

Code: Select all

.
..
foo
bar
baz
and you're looking for the "foo" directory. Try stepping through the code in your head and see if you can't figure out the problem.


Also, I don't see why you're writing a function for this. is_dir() is totally sufficient.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: directory exist problem on different php versions

Post by Weirdan »

tasairis wrote:is_dir() is totally sufficient.
Not totally, because you also need is_link() from time to time.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: directory exist problem on different php versions

Post by requinix »

Weirdan wrote:
tasairis wrote:is_dir() is totally sufficient.
Not totally, because you also need is_link() from time to time.
I meant for his purposes. is_file() and file_exists() are important too.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: directory exist problem on different php versions

Post by Weirdan »

tasairis wrote:is_file() and file_exists() are important too.
No, I meant we don't know where he uses that check. For example, if you check dir for existence before attempting recursive delete, you want to distinguish between a link to a directory and directory itself (both return true for is_dir() call) and run unlink() for link and recursive delete for real directories.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: directory exist problem on different php versions

Post by requinix »

Weirdan wrote:
tasairis wrote:is_file() and file_exists() are important too.
No, I meant we don't know where he uses that check. For example, if you check dir for existence before attempting recursive delete, you want to distinguish between a link to a directory and directory itself (both return true for is_dir() call) and run unlink() for link and recursive delete for real directories.
Okay, that's true. But I'm just looking at his existing code. That particular function can be replaced with a single call to is_dir.
User avatar
MindOverBody
Forum Commoner
Posts: 96
Joined: Fri Aug 06, 2010 9:01 pm
Location: Osijek, Croatia

Re: directory exist problem on different php versions

Post by MindOverBody »

tasairis wrote:
Weirdan wrote:
tasairis wrote:is_file() and file_exists() are important too.
No, I meant we don't know where he uses that check. For example, if you check dir for existence before attempting recursive delete, you want to distinguish between a link to a directory and directory itself (both return true for is_dir() call) and run unlink() for link and recursive delete for real directories.
Okay, that's true. But I'm just looking at his existing code. That particular function can be replaced with a single call to is_dir.
Yap, works fine this way. Simple solution is almost allways the best solution :D
Post Reply