4

Everything is working dandy except for when I upload a file and give it it's own name (via a text field in a html form) "Åäöô - KARL".

I have made a custom fileupload/handler/cms for my site and everything works except for when the file is uploaded.

When I look directly at the file upload directory via my ftp-program the file name is all of a sudden "Åäöô - KARL".

  • I run html5 with <meta charset="UTF-8" /> at the start of every page
  • I encode while writing code with UTF-8 Without BOM

I've tried iconv(), is there something I'm missing? I'm suspecting it's between the html form $_POST['name_of_file'] and when I run the command

move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_POST['name_of_file'])

Progress: I actually did a "utf8_decode" and now the file is saved as the correct name, it's displaying weird on my site though now, so I have to encode it when I want to display it.

Anyway to get around having to type utf8_encode and utf8_decode everywhere??

For others this post might help: How to handle user input of invalid UTF-8 characters?

Community
  • 1
  • 1
basickarl
  • 37,187
  • 64
  • 214
  • 335

1 Answers1

4

PHP filesystem functions are known to mangle non-ASCII filenames.

I suggest you strip/convert all non-ASCII characters or, if that's not possible, try to utf8_[en/de]code.

See also How to handle user input of invalid UTF-8 characters?.

Community
  • 1
  • 1
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
  • Yep the decode/encode is working now, but it seems so messy to have them everywhere! – basickarl Mar 03 '13 at 15:11
  • So have one class (or set of functions) that handles the file upload, and parameterise that code so that you can use it for as many purposes as you need. – leftclickben Mar 03 '13 at 15:17
  • @KarlMorrison: Don't be fooled tho! The `utf8_[en/de]code` functions will only work with ISO-8859-1 charsets (like the one you're testing). Other charsets will fail, and for some not even `iconv()` or `mb_convert_encoding()` will work. If you go down this road, be sure to test your approach under different OSes, in writing and reading conditions as well with `glob()`, `scandir()` and friends. – Alix Axel Mar 03 '13 at 15:23