How do I save the downloaded files with a non-ASCII name?
I can not tell you the name of the file UTF-8 in move_uploaded_file()
because it is converted in turn, resulting in an erroneous name in the file system. For example:
move_uploaded_file($_FILES['userfile']['tmp_name'], '\upload\é.jpg');
creates xa9.jpg in the download directory.
Although the Windows API supports UTF-16 , passing such a filename (for example iconv('UTF-8', 'UTF-16', 'é')
) does not move_uploaded_file()
result in an error.
It would be wise to percent-encode all special characters, and I should definitely do the same with the URI as per RFC 3986 . But when I use percentage encoded URIs, Apache gives a 404 error as it decodes the url and cannot find anything by that name.
For example: <img src="/upload/%C3%A9.jpg" />
gives Apache error:
File does not exist: [...] / upload / \ xc3 \ xa9.jpg.
What's the right decision? If I rename the file on Windows (é.jpg) the HTML URI encoded (% C3% A9.jpg) works as expected.
Some information on the topic: http://www.rooftopsolutions.nl/blog/filesystem-encoding-and-php
source to share
Passing iconv('UTF-8', 'Windows-1250', $_FILES['userfile']['name'])
in move_uploaded_file()
, as opposed to using UTF-16, and storing the filename for HTML as rawurlencode($_FILES['userfile']['name'])
.
If this filename is stored in the database, any file must reference iconv('UTF-8', 'Windows-1250', rawurldecode($filename))
.
I am using the Windows-1250 charator kit as this is the default on my system.
More info on MSDN:
-
File and Directory Names (Naming Conventions)
source to share