【问题标题】:special characters UTF-8 with folders带有文件夹的特殊字符 UTF-8
【发布时间】:2017-09-30 18:03:50
【问题描述】:

我有这个来自 jQuery File Tree PHP Connector 的 php 代码

这个 jQuery 文件树更新了 dir 中的所有文件,并且可以看到里面的所有东西

jQueryFileTree链接Here

此代码中的好消息是使用具有 UTF-8 特殊字符的文件 但坏消息是它不适用于具有特殊字符的文件夹

当我点击文件中的普通文件夹时有特殊字符 = 工作,我可以看到所有文件

当我点击文件夹时,文件中有特殊字符 = 不工作,我看不到任何文件

<?php
//
// jQuery File Tree PHP Connector
//
// Version 1.01
//
// Cory S.N. LaViska
// A Beautiful Site (http://abeautifulsite.net/)
// 24 March 2008
//
// History:
//
// 1.01 - updated to work with foreign characters in directory/file names (12 April 2008)
// 1.00 - released (24 March 2008)
//
// Output a list of files for jQuery File Tree
//

$_POST['dir'] = rawurldecode((isset($_POST['dir']) ? $_POST['dir'] : null ));

if( file_exists($_POST['dir']) ) {
   $files = scandir($_POST['dir']);
   natcasesort($files);
   if( count($files) > 2 ) { /* The 2 accounts for . and .. */
      echo "<ul class=\"jqueryFileTree\" style=\"display: none;\">";
      foreach( $files as $file ) {
         $Path = $_POST['dir'] . $file;
         if(!file_exists($Path) || $file == '.' || $file == '..') {
            continue;
         }
         //The following lines will take care of character encoding of special characters on a windows server.
         //Since special characters (é for example) are not displayed correctly when a file containing this character is found on a windows server.
         //The windows-1252 encoding returned by scandir() does not display correctly in HTML, so we need to convert it to UTF-8
         //See http://stackoverflow.com/questions/22660797/ for full details
         //WARNING: If you have a script running (instead of directly linking to files) to send your scripts, you have to run a reverse encoding conversion over the string passed by jQueryFileTree
         //Use the following line to revert the encoding conversion:
         // $FilePath = iconv(mb_detect_encoding($FilePath),"WINDOWS-1252",$FilePath);
         $file = iconv(mb_detect_encoding($file,array("WINDOWS-1252","UTF-8"),true),'UTF-8',$file);
         $Dir = iconv(mb_detect_encoding($_POST['dir'],array("WINDOWS-1252","UTF-8"),true),'UTF-8',$_POST['dir']);
         $RelString = htmlentities($Dir.$file);
         if(is_dir($Path)) {
            //Handle directories
            echo "<li class=\"directory collapsed\"><a href=\"javascript:void(0);\" rel=\"{$RelString}/\">" . htmlentities($file) . "</a></li>";
         }
         else {
            //Handle files
            $ext = preg_replace('/^.*\./', '', $file);
            echo "<li class=\"file ext_$ext\"><a href=\"javascript:void(0);\" rel=\"{$RelString}\">" . htmlentities($file) . "</a></li>";
         }
      }
      echo "</ul>";  
   }
}

?>

【问题讨论】:

    标签: php jquery file utf-8 tree


    【解决方案1】:

    我发现的解决方法是使用 base64 对完整路径进行编码

    所以 rel= 应该包含 base64_encode($Dir.$File)

    当然在服务器端,你必须做一个base64_decode

    有了这个,任何字符编码都没有问题

    第 72 行必须在 jqueryFileTree.js 文件中进行更改

    showTree( $(this).parent(), escape($(this).attr('rel').match( /.*\// )) );
    

    变成

    showTree( $(this).parent(), $(this).attr('rel') );
    

    【讨论】:

      猜你喜欢
      • 2014-06-15
      • 2011-02-21
      • 1970-01-01
      • 2021-02-23
      • 2015-01-15
      • 1970-01-01
      • 2015-04-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多