Transliteration of Russian-language filenames

Trying to organize multi-upload of files to the server, I encountered a problem that Russian-language file names are saved as a broken encoding (that is, letters are replaced by hieroglyphs), I tried to implement transliteration of Russian-language names, but the function I wrote cannot translate, please tell me , which may be the problem.

HTML code of the page:

<html><head>
<script type="text/javascript" src="jquery.js"></script> 
<script type="text/javascript" src="script.js"></script>
<body>
<form  name="upload"  action="upload.php" method="post" enctype="multipart/form-data">
<div id="files">
<input name="file[]" type="file" value="" class="1" onChange="make_input(this);">
<input  type="submit" value="" >
</div>
</form>
</body>

JS script code:

function make_input(el) {
    if ($(el).attr("class") == 1) {
        $('<input name="file[]" type="file" value="" class="1" onchange="make_input(this);"><br>').prependTo("#files");
        $(el).attr("class", "2");
    }
};

Upload.php form handler code:

<?php
include("1.php");
$uploaddir=";";
$count_file=is_array($_FILES['file']['name'])?count($_FILES['file']['name']):0;
    if($count_file>0)
    {
        for($i=0;$i<$count_file;$i++)
        {
            if($_FILES['file']['error'][$i]==0)
            {
            transl($_FILES['file']['name'][$i]);
                $name_file=basename($_FILES['file']['name'][$i]);
                $uploadfile = $uploaddir.$name_file;
                if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $uploadfile))
                {
                    $file_upload['file_upload'][]=$_FILES['file']['name'][$i];
                }
                else
                {
                    $file_upload['error'][]=$_FILES['file']['name'][$i];
                }
            }
        }
    }
echo "<ul>  :";
if(is_array($file_upload['file_upload']))
foreach($file_upload['file_upload'] as $var)    
{
echo "<li>{$var}</li>";    
}
echo "</ul>";
echo "<ul>   :";
if(is_array($file_upload['error']))
foreach($file_upload['error'] as $var)    
{
echo "<li>{$var}</li>";    
}
echo "</ul>";
echo ($file_upload);
?>

And the actual code of the file with the transliteration function 1.php:

<?php   
    function transl($text) { 
        $trans = array( 
            "" => "a", 
            "" => "b", 
            "" => "v", 
            "" => "g", 
            "" => "d", 
            "" => "e", 
            "" => "e", 
            "" => "zh", 
            "" => "z", 
            "" => "i", 
            "" => "y", 
            "" => "k", 
            "" => "l", 
            "" => "m", 
            "" => "n", 
            "" => "o", 
            "" => "p", 
            "" => "r", 
            "" => "s", 
            "" => "t", 
            "" => "u", 
            "" => "f", 
            "" => "kh", 
            "" => "ts", 
            "" => "ch", 
            "" => "sh", 
            "" => "shch", 
            "" => "y", 
            "" => "e", 
            "" => "yu", 
            "" => "ya", 
            "" => "A", 
            "" => "B", 
            "" => "V", 
            "" => "G", 
            "" => "D", 
            "" => "E", 
            "" => "E", 
            "" => "Zh", 
            "" => "Z", 
            "" => "I", 
            "" => "Y", 
            "" => "K", 
            "" => "L", 
            "" => "M", 
            "" => "N", 
            "" => "O", 
            "" => "P", 
            "" => "R", 
            "" => "S", 
            "" => "T", 
            "" => "U", 
            "" => "F", 
            "" => "Kh", 
            "" => "Ts", 
            "" => "Ch", 
            "" => "Sh", 
            "" => "Shch", 
            "" => "Y", 
            "" => "E", 
            "" => "Yu", 
            "" => "Ya", 
            "" => "", 
            "" => "", 
            "" => "", 
            "" => "" 
        ); 
        if(preg_match("/[--a-zA-Z\.]/", $text)) { 
            return strtr($text, $trans);
        } 
        else { 
            return $text;                  
        }; 
    } 
?>

Thank you!
Any help would be very welcome.


Answer 1, authority 100%

strtr accepts strings, not arrays. Use str_replace

str_replace ( mixed $search , mixed $replace , mixed $subject);

Answer 2, authority 100%

You use the transliteration function, but you don’t save the result anywhere.

transl($_FILES['file']['name'][$i]);

And you need, for example:

$name = transl($_FILES['file']['name'][$i]);

and beyond

$uploadfile = $uploaddir.**$name**;

Answer 3, authority 100%

In this case, capital letters can be removed from the $trans array. Or leave everything as it is, but remove strtolower from the script.

$name = preg_replace('/[^a-z0-9()-\.\+]/','', strtr(strtolower(trim($text)), $trans));
if($name != '') {
  return $name;
} else {
  return false;
}

Answer 4

most likely the file itself containing the array is stored in one encoding, and the uploaded file in another

dig aside multibyte string manipulation
and iconv

+depending on the settings on the server (php.ini) you will find a suitable option that works for YOU!

working example function:

function transl($st,$code='utf-8'){
$st = mb_strtolower($st, $code);  
$st = str_replace(array(  
        '?','!','.',',',':',';','*','(',')','{','}','%','#','№','@','$','^','-','+','/','\\','=','|','"','\'',  
        '','','','','','','','','','','',  
        '','','','','','','','','','','',  
        '','','',' ','','','','','','','',''  
    ), array(  
        '','','','','','','','','','','','','','','','','','','','','','','','','',/*remove bad chars*/  
        'a','b','v','g','d','e','e','z','i','y','k',  
        'l','m','n','o','p','r','s','t','u','f','h',  
        'j','i','e','_','zh','ts','ch','sh','shch',  
        '','yu','ya'  
    ), $st);  
return $st;  
}   
echo transl("    @$^*^&()&(5");