WiLDRAGoN
WiLDRAGoN

Reputation: 79

Dealing With Single Quote In Filename

I have this code that finds the flag ( which is a GIF file in "/flags" directory) for a country and displays the flag, but I have a problem with a particular country containing a ' (single quote or apostrophe) in the filename.

$country = "australia";
$flag = (glob("flags/$country.gif"));

The flags are named exactly the same as the country name ($country), ie: australia.gif.

This code works fine except when it encounters the country "Cote d'Ivoire".

$country = "cote d'ivoire";
$flag = (glob("flags/$country.gif"));

I end up with the following URL for the flag GIF. "/flags/cote%20d"

How do I work around this '?

I've tried htmlspecialchars and urlencode to no avail.

I made a workaround creating a new GIF flag file named "cotedivoire.gif" and added the following code, but this is not ideal. Surely there has to be a better solution.

if ($country == "cote d'ivoire") {
    $flag = (glob("flags/cotedivoire.gif"));
}

Any ideas please?

Upvotes: 1

Views: 3548

Answers (1)

Snickbrack
Snickbrack

Reputation: 956

You have to escape the quotes in your filename string like this:

Example: "file'n/ame.jpg";
Solution: "file\'n\/ame.jpg"

Upvotes: 1

Related Questions