file name with special characters like “é” NOT FOU

2019-05-10 23:48发布

问题:

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?

回答1:

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.



回答2:

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