tags:

views:

350

answers:

2

Hey there lovely stackoverflow people!

I have got directories which contain brackets in the names. i.e. "dir_123[[email protected]]"

Within that dirs there are .tif files.

What I do is counting the Tif files. On my Mac I did that with MAMP and it worked great:

$anz = count(glob(str_replace("[", "\[", "dir_123[[email protected]]/*.tif")));

On my Windows machine running XAMPP it won't work because of that brackets:

$anz = count(glob(str_replace("[", "\[", "dir_123[[email protected]]\\*.tif")));

How can I get my XAMPP Server to read that directories?

thx, Max

+1  A: 

Have you tried to escape all the special characters?

Ex.

$dir = "dir_123[[email protected]]";

$from = array('[',']');
$to   = array('\[','\]');

$anz = count(glob(str_replace($from,$to,$dir . "\\*.tif")));

This works for me on Ubuntu.

If that ain't working you can do:

function countTif($dir) {
    $ret = 0;
    $scan = scandir($dir);
    foreach($scan as $cur) {
        $ret += ((substr($cur,-4) == ".tif")?1:0);
    }
    return $ret;
}

And if you need recursive counting:

function countTif($dir) {
    $ret = 0;
    $scan = scandir($dir);
    foreach($scan as $cur) {
        if(is_dir("$dir/$cur") and !in_array($cur,array('.','..'))) {
            $ret += countTif("$dir/$cur");
        } else {
            $ret += ((substr($cur,-4) == ".tif")?1:0);
        }
    }
    return $ret;
}

This functions was tested and worked on my Ubuntu 9.04 computer with php 5.2.6-3ubuntu4.1

Hope it works for ya!

//Linus Unnebäck

Linus Unnebäck
thx for your suggestions, Linus Unnebäck!I am sure, they'd worked as well!
Max
A: 

I solved it by using this code instead:

$dir = scandir("\\server\dir\");
 foreach ($dir as $key=>$row){
  if(end(explode(".", $row)) != "tif"){
   unset($dir[$key]);
  }
 }
$anz = count($dir);
Max
your double quotes are unbalanced, my friend.
Henrik Paul
what do mean with unbalanced?
Max