views:

281

answers:

2

I have a folder on my website just for random files. I used php opendir to list all the files so i can style the page a bit. But the files that I uploaded with special characters in them don't work. when i click on them it says the files are not found. but when i check the directory, the files are there. seems like the links are wrong. any idea how i can get a correct link to these file names with special characters in them?

A: 

do you see them if you run this?

foreach (new DirectoryIterator('/path/to/folder') as $fileInfo) {
    if($fileInfo->isDot() || $fileInfo->isDir()) continue;
    echo $fileInfo->getFilename() . "<br>\n";
}

EDIT: just realised i misread the question. Its likely some kind of encoding issue like porneL says

seengee
+2  A: 

This is tricky. It depends what encoding your filesystem uses for filenames and how (if) your webserver or PHP functions convert the encoding.

First of all, make sure your links never use unencoded non-ASCII characters. URLs should be in UTF-8, i.e. é should be encoded as %C3%A9. If that doesn't work, try %E9 (é in ISO-8859-1).

You might find iconv() function useful to convert encodings. rawurlencode() is obligatory.

porneL
i tried:print('<li><span class="count">' . $count . '.</span> <a href="/files/' . iconv_set_encoding($file, "UTF-8") . '">' . iconv_set_encoding($file, "UTF-8") .'</a></li>' . "\n");But the links are now just all empty, like this: <a href="/files/"></a></li>
Eric
rawurlencode() worked! :)
Eric