strange behavior

From: Date: Sun, 16 Jul 2006 20:46:47 +0000
Subject: strange behavior
Groups: php.general 
Request: Send a blank email to php-general+get-239510@lists.php.net to get a copy of this message
Hello: I have a directory on a server that has php code that creates and writes to directories as such; /tiles_.*/. Each directory with a matching name has graphics files written to it. i'm trying to write a script the will eliminate the graphics files and the directories they are in (tiles_.*). The present code is: <?php function prune()
              {
               $f = 0;
               $pat = 'tiles_.*';
               $view = array();
               $t = array();
               $str = '';
               $start = opendir('.');
               //print"Hello???<br>"; debug code
               while($lst = readdir($start))
                   {
                    if(ereg($pat, $lst))
                      {
                       $t[$f] = $lst;
                       $str .= $t[$f].'<br>'; //debug code
                      }
                     $f++;
                    }
               rewind($start);
               closedir($start);
               //print $str; debug code----the array $t has all valid values at this point.
               for($f = 0; $f < count($t); $f++)
                 {$tmp = $t[$f];
                  chdir($tmp);
                  $dir = opendir($tmp);
                  $r = 0;
                  while($i = readdir($dir))
                       {
                        @unlink($i);
                        $r++;
                       };
                   rewind($dir);
                   closedir($dir);
                   unset($dir);
                   chdir('../');
                   rmdir($tmp);
                  }
              }
prune(); ?> The problem is that each time the for loop tries to chdir to an array $t item, errors regarding invalid resources and none existent files are generated. If I use chdir('./'.$tmp) and opendir('./'.$tmp) the code removes all the files in the directory the script is located in, including the script file itself. So, I am thinking that, in this case, the directories aren't even changed when the @unlink() line is reached and unlink() only sees the current directory files and not included directories, and removes everything that isn't a directory. Here is a sample of the errors generated: Warning: chdir() [function.chdir]: No such file or directory (errno 2) in (path)/prune.php on line 30 // (all resources used based on this are invalid) and; Warning: opendir(tiles_9215fd05) [function.opendir]: failed to open dir: No such file or directory in /var/www/html/exp/tile_puzzle/prune.php on line 31 (this happens on the second iteration of the loop......tiles_9215fd05 is a valid array $t item) So, Can someone tell me what I might be doing wrong here. I don't want to swim around with any more trial and error if possible. I'm using php 5.1.2, Apache 1.3.34 Thanks in advance. JK

« previous php.general (#239510) next »