Search code examples
phplinuxfilenamesnon-ascii-characters

How to open this file?


Unfortunately I've allowed users to upload files to the server without sanitizing its names first (Linux, PHP, MySQL).

One of the uploaded filename is "E_M_20-06-2013_14-15_ComidasTípicas.zip" (it's correct in the MySQL database, UTF-8 encoded).

But when I try do a fopen() in PHP I can not find this file. Depending on the locale (LANG) I retrieve different filenames, as such:

    $ export LANG=pt_BR.UTF-8
    $ ls ../web/downloads/E_M_20-06-2013_14-15_ComidasT*
    ../web/downloads/E_M_20-06-2013_14-15_ComidasTípicas.zip

    $ export LANG=pt_BR.ISO-8859-1
    $ ls ../web/downloads/E_M_20-06-2013_14-15_ComidasT*
    ../web/downloads/E_M_20-06-2013_14-15_ComidasT▒?­picas.zip

    $ export LANG=C                             
    $ ls ../web/downloads/E_M_20-06-2013_14-15_ComidasT*
    ../web/downloads/E_M_20-06-2013_14-15_ComidasT????picas.zip

Can someone help me in discovering how can I open this file using PHP?

PS: of course, I've tried utf8_encode, utf8_decode, no sucess.

Thanks in advance!


Solution

  • Write a script that finds files and renames the file without corrupted characters (non-alphanumeric and underscores)

    Check scandir to find the files http://php.net/manual/en/function.scandir.php

    Check this thread to find non-alphanumeric characters PHP Regular expression - Remove all non-alphanumeric characters

    So, you will get clean file names and as written on your database. If needed you can do the same operation on your database field too.