case insensitive file_exists()?

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
MM Techniques
Forum Newbie
Posts: 1
Joined: Mon Mar 31, 2003 5:38 pm
Location: Virtual Memory

case insensitive file_exists()?

Post by MM Techniques »

Is there a way to have the file_exists() function do a case insensitive search? I have alot of people who's links (either favorites or search engines) have all lowercased links. My site uses a simple include script like index.php?ServicePage=ClutchNew . If someone for say did a ServicePage=clutchnew I'd like the page to still display. Besides building an array with over a thousand values, one for each page; is there a way to do a case insensitive search? maybe something like:

---- Old Code

Code: Select all

if (eregi("%", $REQUEST_URI)){
$uri = urldecode(rawurldecode($REQUEST_URI));
header ("Location: http://www.mechanicmatt.com$uri");
}
else {}
$ServicePage = ereg_replace(".php", "", ereg_replace(".mm", "", $ServicePage));

if ($ServicePage == "" || !isset($ServicePage)){
$IncludeFile = "Main";
}
else if (file_exists("$ServicePage.php")){
$IncludeFile = "$ServicePage";
}
else{
    //if ($HTTP_REFERER == "" || eregi("unknown", $HTTP_REFERER)){
    urlencode(rawurlencode($REQUEST_URI));
    header("Location: http://www.mechanicmatt.com/MMIntelligence.php?RURI=$REQUEST_URI;$HTTP_REFERER");
    //}
    //else {
    //$IncludeFile = "e404";
    //}
}
To New Code...

Code: Select all

else if (strtolower(file_exists(strtolower("$ServicePage.php")))){
$IncludeFile = "$ServicePage";
}
Tubbietoeter
Forum Contributor
Posts: 149
Joined: Fri Mar 14, 2003 2:41 am
Location: Germany

Post by Tubbietoeter »

If you work with unix I don't think this is possible ... since Unix just is case sensitive.
But you could give your pages numbers or name them all lowercase or you could realize a file_exists function of your own

but this:
if (strtolower(file_exists(strtolower("$ServicePage.php")))){
$IncludeFile = "$ServicePage";
}


won't work. Because what you do is you search for a lowercase filename which probably does not exist and change the return-value of the file_exists() function to lowercase.


you also could generate a mapping file which has as first parameter the lowercase filename like the one you get from the users an second parameter the filename for the file on the system (eg "lowercasefilename@FileNameInReal"). depending on names convention etc pp you could do an awk script that automatically generates the list every hour or so.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

It is easiest if you have all of your page names as lowercase, that way you can just strtolower() the $ServicePage variable to do the check.

Mac
Post Reply