PHP FTP使用utf8编码获取文件夹名称

时间:2012-08-02 10:27:44

标签: php utf-8 ftp

我只是不能让它工作,基本上我在ftp服务器上显示文件,工作正常,但一些文件夹名称有德语字符,如äöü或法语字符。我将下面的行放入php代码中,看它是否有帮助,但它只能删除法语字符,但是德语字符仍然有 图标而不是实际字符......

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>

2 个答案:

答案 0 :(得分:2)

如果可能(如果FTP服务器支持)尝试呼叫:

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

此命令为目录列表启用UTF-8编码(ftp_nlist)。

答案 1 :(得分:0)

好的,我已经制定了一项可能对其他人有用的工作:

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

这不是最好的方法,但它适用于我,如果你对网址进行编码,它会将尴尬的字符更改为例如%82 ...然后,您可以使用HTML代码替换它们。