acctman
acctman

Reputation: 4349

proper usage of glob()

Is this the correct way to us glob() i'm trying to do a case insensitive search for the folder TestFolder on the server.

$chid = "testFoLdER";
$dirchk2 = "/temp/files/" . glob('".$chid."') . "/" . $data[1] . ".doc";

@code_burgar I made these changes to apply to the example code_burgar showed me. is this correct? what i'm trying to do here is what ever globistr find for casing, rename the folder to lowercase.

$chid = (strtolower($_POST['chid']));
$findbatch = globistr($chid);
$results = glob($findbatch);

if ( !empty($results) ) {
  $result = $results[0];
  rename("/temp/files/" . $results . "/", "/temp/files/" . strtolower($chid) . "/");
} 
else 
{
    $missing_dir = 'Folder containing files, Not Found: ' . $chid . "\r";
    $errfile = fopen("/rec/" . $chid . "-errlog.txt", "a");
        fwrite($errfile, $missing_dir . "\n");
        fclose($errfile);
        exit();                           
}

Upvotes: 4

Views: 599

Answers (2)

dev-null-dweller
dev-null-dweller

Reputation: 29492

As workaround you can search all folder inside /temp/files/ that contain $data[1]. '.doc' file and then loop through results to make case-insensitive check if path contains your folder.

$file = "/temp/files/*/".$data[1].".doc";
$locations = glob($file);
$found = false;

foreach($locations as $l){
    if(stripos($l,'/testfolder/') !== false){
        $found = $l;
        break; 
    }
}

Upvotes: 1

code_burgar
code_burgar

Reputation: 12323

That is most definitely not the way to use glob(). glob() returns an array and you are trying to use it in string concatenation.

As Pekka pointed out, PHP man page for glob has some case-insensitive example code.

What you are looking for basically is something along these lines (globistr() comes from PHP man page comments):

$chid = globistr("testFoLdER");
$results = glob($chid);

if ( !empty($results) ) {
  $result = $results[0];
  $dirchk2 = "/temp/files/" . $result . "/" . $data[1] . ".doc";
} else {
  echo('Not found');
}

Upvotes: 1

Related Questions