0

I'm looking for files in a directory. My directory pathname pattern looks like this:

  • TEST_1.jpg

  • TEST_2.jpg

  • TEST_3.jpg

  • TEST_4.jpg

  • twPL_1.jpg

  • twPL_2.jpg

  • twPL_3.jpg

  • twPL_4.jpg

so if i have an URL Parameter with localhost:8000/covers?customer=twPL. I'm getting the first two jpg's and this is working. The problem is that the URL Parameter is case sensitive. If i try to write the URL Paramater with lower case characters (...?customer=twpl) i'm getting a notice :

Notice: Undefined offset: 0

Notice: Undefined offset: 1

<?php
        if(isset($_GET["customer"])) {
            $customer = $_GET["customer"];
            $dirs = glob("cover/".$customer."*.jpg");
            $allData = glob("cover/*.jpg");
            $killExt = array("cover/","_1","_2","_3","_4",".jpg");
            $new_array = str_replace($killExt, "", $allData);
            $arrayLowerCase = array_map('strtolower', $new_array);

            // die(var_dump($dirs));

            if(array_search(strtolower($customer), $arrayLowerCase) !== false){
                for($i=0; $i < 2; $i++) {
                    echo 
                    '<div class="cover" data-page-number="0">',
                        '<img class="front-modal" src="'.strtolower($dirs[$i]).'"/>',
                    '</div>';
                }
            }   
        }
    ?>

I think this causes the problem $dirs = glob("cover/".$customer."*.jpg"); how can i search in this glob function in a case insensitive way...

Greg Ostry
  • 1,161
  • 5
  • 27
  • 52

1 Answers1

0

I think the best way is to start with a rule than you define : name in lower or in caps. So, it's easier to just lowercase all $_GET parameters than doing what you want to do. Whatever.

For your problem, you can, by regexp, search all the patern matching your two files name : TEST : Test, TEsT, etc twPL : twpl, TWPL etc

//this will find all pattern of twpl
preg_match("/twpl/i", $_GET["customer"], $matches_twpl);
preg_match("/test/i", $_GET["customer"], $matches_test);

and then

$safe_cust = "";
if(strtolower($matches_twpl[0]) == "twpl"){
    $safe_cust = "twPL";
} else if ( strtolower($matches_test[0]) == "test") {
    $safe_cust = "TEST"; 
}else {
    //error
}
Eiji
  • 454
  • 6
  • 15