2

My question is: how can/should I create a directory, test if exists and get it's contents using special characters in the name?

My scenario: I need to create a directory, put some images there, and then get that directory contents. That directory may contain a special characters. For example galería

When creating the directory with mkdir(), the name changed to galería. So the solution that worked for me was to utf8_decode() the string.

So far so good...

I upload images to that directory...

Then I attempted to scan the directory doing:

$gallery = [];

if(is_dir($path)) //$path contains a valid path including my recently created directory
{
    $gallery = glob("$path*{.jpg,.gif,.png}", GLOB_BRACE);
}

echo "<pre>";
print_r($gallery);
echo "</pre>";

But is_dir() returned false. So I had to do this to get inside the if statement:

$path = mb_convert_encoding($path, "ISO-8859-1", "UTF-8");

Now is_dir() works, but glob() doesn't. At this point I should mention that after mb_convert_encoding(), the name I'm getting is galer�a

Any help?

EDIT: If I use pictures for directory name, I don't need to use utf8_decode() neither mb_convert_encoding(). mk_dir(), is_dir() and glob() work flawlessly

Matías Cánepa
  • 5,770
  • 4
  • 57
  • 97
  • did you try mb_internal_encoding('utf-8'); ? – ziollek Feb 26 '14 at 21:23
  • @ziollek: What I've tried is what I posted plus a combination of utf8_encode, utf8_decode, urlencode, and urldecode functions mixed together. I will search for the function you suggest – Matías Cánepa Feb 26 '14 at 21:27
  • 1
    [some relevant reading material for you](http://evertpot.com/filesystem-encoding-and-php/): as soon as you step out of the ascii-range, it is varies per OS (and filesystem). – Wrikken Feb 26 '14 at 21:29
  • @Wrikken: I was starting to think something like that. I'm currently developing in Windows 7 and was wondering if the same happens in Linux. If that's the case: is there a cross-platform solution? – Matías Cánepa Feb 26 '14 at 21:34
  • @Wrikken: Should I "simply urlencode all your filenames before writing them to disk" like the article says? Although urlencode() aint doing it for me – Matías Cánepa Feb 26 '14 at 21:34
  • Well, I mainly linked to it to show you the issues between operating systems. I have never had a real reason to step outside ascii for path or filenames. Ever. `urlencode()`-ing them isn't very elegant in my opinion, I usually just cast it to ascii with `iconv`, and query & link them by that name. If you _need_ to make a distinction between the directory 'gap' & 'gäp', well, good luck, but I personally would usually find the ascii solution for it. – Wrikken Feb 26 '14 at 21:41
  • Solution: don't use special characters in file/folder names. It is a nightmare without end. – Sammitch Feb 26 '14 at 21:46
  • @Sammitch: I'm starting to see that, but I must support Spanish characters like á, ü and ñ – Matías Cánepa Feb 26 '14 at 21:48
  • 2
    Must you? Replace them with [transliteration](http://stackoverflow.com/questions/1284535/php-transliteration) to conform to ASCII naming convention, and append a short hash of the original name to prevent collisions. eg `Matías -> Matias-56dbe8b4` where the hash is from `dechex(crc32("Matías"))` – Sammitch Feb 26 '14 at 21:55

0 Answers0