3

I have a classic php script for saving tmp files:

$uploads_dir = "scans";
var_dump( $_FILES );
foreach ($_FILES["pictures"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
        $name = $_FILES["pictures"]["name"][$key];

        rename($tmp_name, urldecode("/var/www/html/$uploads_dir/$name") );
    }
}
echo urldecode("/var/www/html/$uploads_dir/$name");

However, the file 'фавикон.png' gets saved as 'фавикон.png'. Please help me out what to do with encoding.

Thank you

EDIT:

Got it working with iconv function. However, for some weird reason it had to be encoded into windows format.

The resulting code:

if ($error == UPLOAD_ERR_OK) {
    $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
    $name = $_FILES["pictures"]["name"][$key];
    $name =iconv('UTF-8','windows-1251',  $name);
    copy($tmp_name, "/var/www/html/$uploads_dir/$name");
}
Goodie123
  • 467
  • 4
  • 16
  • Do you mean the result of temp name is not the same as the name you uploaded it as? This is expected behaviour, and a good thing too. It is preferable not to store the file on the disk with a name a user gave you because they could be up to no good. Ask for the name seperately and store it in DB with a reference to the file once you have moved it out of temp directory. (Assuming that's what you mean) – DanielM Feb 11 '16 at 07:44

3 Answers3

0

You might need the following setlocale(LC_ALL, 'ru_RU.utf8')

JazzCat
  • 4,243
  • 1
  • 26
  • 40
0

try to put this line on the top of the script:

header('Content-Type: text/html; charset=utf-8');
  • 1
    While a code only answer solves the problem for the op, it isn't recommended as it provides no value for future visitors, a answer that only provides is quickly going to be flagged as "very low quality" and by the result of that it will be deleted quickly. [edit] your answer to include an explanation what the provided code does. – Ferrybig Feb 11 '16 at 09:23
0

Have you try to use utf8_decode:

utf8_decode($name);

I suggest that you change the name of the file, there are very good reasons to do this. Check this post for more details: https://stackoverflow.com/a/17866898/1016425

Community
  • 1
  • 1
lloiacono
  • 4,714
  • 2
  • 30
  • 46
  • I just tried it. Didnt work. I am trying to do a thing where user can view the files later on so if the file is renamed its gonna be tough for the user to search for the file. – Goodie123 Feb 11 '16 at 07:49
  • I suggest you still rename the file and keep stored somewhere the mapping between the new filename and path and the original file name. – lloiacono Feb 11 '16 at 07:50