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

HTML code for 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 translate 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! I would be very happy with any help.

    4 answers 4

    strtr accepts strings, not arrays. Use str_replace

     str_replace ( mixed $search , mixed $replace , mixed $subject); 
    • In my case, str_replace(xxxx , $trans, $text) , where: $text is the original file name; $trans - set of characters to replace; and what should be in place xxxx? - 3axap
    • A set of replaceable characters. That is, str_replace (array_keys ($ trans), array_values ​​($ trans), $ text). And don't forget to write the result in $ name_file. - ling
    • I did the following: I created an array with Russian letters; I created an array with transliteration; I designed the function as follows: $ new_name = str_replace ($ rus, $ eng, $ text); where: $ rus is an array with Russian letters; $ eng - array with transliteration Result: unresolved replacement text. - 3axap
    • The issue is resolved. Changing the encoding of the file with the function of translit. But using the function offered by you. Thanks for the help. - 3axap

    You use the transliteration function, but its result is not saved anywhere.

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

    And you need, for example:

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

    and further

     $uploadfile = $uploaddir.**$name**; 
    • The fact is that in the function itself I did intermediate conclusions, and it always turns out to be an empty variable, the function simply refuses to understand Russian letters, I already begin to think, maybe the whole problem is in the encoding ... - 3axap
    • I tried anyway, as you suggest, the result is not working, it transmits the original name not past transliteration ... - 3axap

    In this embodiment, large letters can be removed from the $ trans array. Either leave everything as it is, but remove it from the strtolower script.

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

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

      dig aside multibyte work with strings and iconv

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

      working function example:

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