简体   繁体   中英

Php saves uploaded files with wrong encoding on linux server

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");
}

您可能需要以下setlocale(LC_ALL, 'ru_RU.utf8')

尝试将此行放在脚本的顶部:

header('Content-Type: text/html; charset=utf-8');

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM