2012-08-02 48 views
0

我只是不能得到這個工作,基本上Im顯示在FTP服務器上的文件,這工作正常,但一些文件夾名稱有德國字符,如äöü或法語字符。我把線下到PHP代碼,看看是否有幫助,但它不僅擺脫了法國的人物,但是德國人物仍然有圖標,而不是實際的字符...PHP FTP獲取utf8編碼的文件夾名稱

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

我會感謝任何幫助。下面是整個文件:

import.php

<html> 
<head> 
    <title>Import</title> 
    <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
    <link type="text/css" rel="stylesheet" href="css/import.css"/> 
    <style type="text/css"> 
     .folder_hilighted table tr td div.name_tag_name{ 
      background: #75B323; 
      color: #ffffff; 
      border-radius: 20px; 
      text-shadow: 0px -1px 0px #5D855A; 
     } 
    </style> 
</head> 
<body><br> 
    <table width="100%" cellspacing="0" cellpadding="0"><tr><td> 
    <ul class="folders"> 
    <?php 
     ini_set("max_execution_time",100000); 
     $folder = $_GET['folder']; 

     // set up basic connection 
     $ftp_server = "xxx.xxx.xx.xx"; 
     $ftp_user_name = "admin"; 
     $ftp_user_pass = "password"; 

     // set up basic connection 
     $conn_id = ftp_connect($ftp_server); 

     // login with username and password 
     $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 

     // get the file list for/
     if($folder==""){ 
      $buff = ftp_nlist($conn_id, '/array1/MediaBox/iTunes/'); 
     } 
     else{ 
      $buff = ftp_nlist($conn_id, '/array1/MediaBox/iTunes/'.$folder.'/'); 
     } 

     // close the connection 
     ftp_close($conn_id); 

     // output the buffer 
     for($i=0;$i<=sizeof($buff)-1;$i++){ 
      if(is_dir('ftp://'.$ftp_user_name.':'.$ftp_user_pass.'@'.$ftp_server.$buff[$i]) && $buff[$i]!="/array1/MediaBox/iTunes/Mobile Applications" && $buff[$i]!="/array1/MediaBox/iTunes/Automatically Add to iTunes.localized" && $buff[$i]!="/array1/MediaBox/iTunes/Books" && $buff[$i]!="/array1/MediaBox/iTunes/Tones"){ 
       if($folder==""){ 
        $folder_name = str_replace(array("/array1/MediaBox/iTunes/"), array(""), $buff[$i]); 
        $folder_name = mb_convert_encoding($folder_name, 'UTF-8', mb_detect_encoding($folder_name)); 
        $folder_name = htmlspecialchars($folder_name, ENT_QUOTES, 'UTF-8'); 
       } 
       else{ 
        $folder_name = str_replace(array("/array1/MediaBox/iTunes/".$folder."/"), array(""), $buff[$i]); 
        $folder_name = mb_convert_encoding($folder_name, 'UTF-8', mb_detect_encoding($folder_name)); 
        $folder_name = htmlspecialchars($folder_name, ENT_QUOTES, 'UTF-8'); 
        $last_folder = str_replace(array("/array1/MediaBox/iTunes/"), array(""), $buff[$i]); 
        $last_folders = explode("/", $last_folder); 
        for($k=0;$k<=count($last_folders);$k++){ 
         if($k==count($last_folders)){ 
          $prev_folder = $last_folders[$k-3]; 
         } 
        } 
       } 
      ?> 
       <li align="center" title="<?php echo $folder_name ?>" id="<?php echo md5($folder_name); ?>" onClick="hilight_folder('<?php echo md5($folder_name); ?>');" ondblclick="change_folder('<?php echo $folder_name; ?>')" style="cursor: pointer; height: 90px;"> 
        <table cellspacing="0" cellpadding="0"> 
         <tr> 
          <td align="center"> 
           <img height="75" style="padding-left: 5px; padding-right: 5px; padding-top: 5px;" src="img/folder.png"> 
           <div class="prev_folder" style="display: none;"><?php echo $prev_folder ?></div> 
          </td> 
         </tr> 
         <tr> 
          <td align="center" class="name_tag"> 
           <div class="name_tag_name" style="margin-top: 2px; width: auto;"> 
            <?php 
             $short_folder = substr($folder_name, 0, 12); 
             if($folder_name == $short_folder){ 

             } 
             else{ 
              $short_folder = $short_folder."..."; 
             } 
             echo $short_folder; 
            ?> 
           </td> 
          </div> 
         </tr> 
        </table> 
       </li> 
      <?php 
      } 
      else if($buff[$i]!="/array1/MediaBox/iTunes/Mobile Applications" && $buff[$i]!="/array1/MediaBox/iTunes/Automatically Add to iTunes.localized" && $buff[$i]!="/array1/MediaBox/iTunes/Books" && $buff[$i]!="/array1/MediaBox/iTunes/Tones" && $buff[$i]!="/array1/MediaBox/iTunes/" && $buff[$i]!="/array1/MediaBox/iTunes/Icon"){ 
       if($folder==""){ 
        $folder_name = str_replace(array("/array1/MediaBox/iTunes/"), array(""), $buff[$i]); 
       } 
       else{ 
        $folder_name = str_replace(array("/array1/MediaBox/iTunes/".$folder."/"), array(""), $buff[$i]); 
        $last_folder = str_replace(array("/array1/MediaBox/iTunes/"), array(""), $buff[$i]); 
        $last_folders = explode("/", $last_folder); 
        for($k=0;$k<=count($last_folders);$k++){ 
         if($k==count($last_folders)){ 
          $prev_folder = $last_folders[$k-3]; 
         } 
        } 
       } 
       ?> 
        <li align="center" title="<?php echo $folder_name ?>" id="<?php echo md5($folder_name); ?>" onClick="hilight_folder('<?php echo md5($folder_name); ?>');" ondblclick="change_folder('<?php echo str_replace(array("/array1/MediaBox/iTunes/"), array(""), $buff[$i]); ?>')" style="cursor: pointer; height: 90px;"> 
         <table cellspacing="0" cellpadding="0"> 
          <tr> 
           <td align="center"> 
            <img height="75" style="padding-left: 5px; padding-right: 5px; padding-top: 5px;" src="img/music_file.png"> 
            <div class="prev_folder" style="display: none;"><?php echo $prev_folder ?></div> 
           </td> 
          </tr> 
          <tr> 
           <td align="center" class="name_tag"> 
            <div class="name_tag_name" style="margin-top: 2px; width: auto;"> 
             <?php 
              $short_folder = substr($folder_name, 0, 12); 
              if($folder_name == $short_folder){ 

              } 
              else{ 
               $short_folder = $short_folder."..."; 
              } 
              echo $short_folder; 
             ?> 
            </td> 
           </div> 
          </tr> 
         </table> 
        </li> 
       <?php 
      } 
     } 
     ftp_close($conn_id); 
    ?> 
    </ul> 
</td></tr> 
<tr><td> 
    <div style="height: 60px;">&nbsp;</div> 
</td></tr></table> 
</body> 
</html> 

回答

0

好啦,我已經制定了圍繞這可能是有用的一個工作別人知道:

str_replace(array('%82','%94','+'),array('&#233;','&#246;',' '),urlencode($folder_name)); 

這是不是最好的方式,但它適用於我,如果你url編碼一個字符串,它會將尷尬的字符變成例如%82 ...然後您可以用HTML代碼替換它們。

+0

我只是'utf8_encode'文件/文件夾名稱,它工作正常 – Teiv 2012-09-19 01:10:27

2

如果這是可能的(如果支持的FTP服務器上)嘗試致電:

ftp_raw($conn_id, "OPTS UTF8 ON"); 

此命令啓用目錄列表(ftp_nlist)UTF-8編碼。