zoukankan      html  css  js  c++  java
  • PHP 支持中文目录和文件的的遍历:文件编码转换

    在使用 readdir() 遍历指定目录时,使中文目录和文件名都正常显示需要使用 iconv() 进行文件编码转换:

     1 <?php
     2 
     3 header("Content-type:text/html;charset=utf-8");
     4 
     5 $num = 0;
     6 $dirname = 'practise';
     7 
     8 $dirname = iconv( 'utf-8', 'gb2312',$dirname );
     9 
    10 $dir_handle = opendir($dirname);
    11 
    12 echo '<table border="0" align="center" width="600" cellspacing="0" cellpadding="0">';
    13 echo '<caption><h2>目录'.$dirname.'下面的内容</h2></caption>';
    14 echo '<tr align="left" background="#ccc">';
    15 echo '<th>文件名</th><th>文件大小</th><th>文件类型</th><th>修改时间</th></tr>';
    16 
    17 while($file = readdir($dir_handle)){
    18 
    19     $file1=iconv('gb2312','utf-8',$file);
    20 
    21     $dirFile = $dirname."/".$file;
    22 
    23     $bgcolor = $num++%2==0?'#fff':'#ccc';
    24     echo '<tr bgcolor='.$bgcolor.'>';
    25     echo '<td>'.$file1.'</td>';
    26     echo '<td>'.filesize($dirFile).'</td>';
    27     echo '<td>'.filetype($dirFile).'</td>';
    28     echo '<td>'.date("Y-n-t",filemtime($dirFile)).'</td>';
    29     echo '</tr>';
    30 }
    31 
    32 echo '</table>';
    33 closedir($dir_handle);
    34 
    35 echo '在<b>'.$dirname.'</b>目录下的子目录和文件共有<b>'.$num.'</b>个';

    在页面中显示:

     目录practise下面的内容

    文件名文件大小文件类型修改时间
    . 0 dir 2014-10-31
    .. 0 dir 2014-10-31
    js 0 dir 2014-10-31
    mysql 0 dir 2014-10-31
    php 0 dir 2014-10-31
    想一点记一点.txt 1975 file 2013-6-30
    正则 0 dir 2014-10-31

    practise目录下的子目录和文件共有7

    否则显示:

                                                             目录practise下面的内容

    文件名文件大小文件类型修改时间
    . 0 dir 2014-10-31
    .. 0 dir 2014-10-31
    js 0 dir 2014-10-31
    mysql 0 dir 2014-10-31
    php 0 dir 2014-10-31
    ��һ����һ��.txt 1975 file 2013-6-30
    ���� 0 dir 2014-10-31

    practise目录下的子目录和文件共有7

    同样,使用 fread() 函数中输出字符时,也可以使用这种方法:

    $fp = fopen('data.txt','r') or die("文件打开失败");
    fseek($fp,-10,SEEK_END);
    echo fread($fp,10)."<br>";

    这段代码的意思是将指针移到倒数第10个字节位置处,并输出最后10个字符

    data.txt如下:

    updating your profile with your name
    2014年10月28日    #
    jQuery 图片剪裁插件初探之 Jcrop
    摘要: 主页:http://deepliquid.com/content/Jcrop.html官方下载地址:http://deepliquid.com/content/Jcrop_Download.html下载包中除了 CSS 文件夹和 js 文件夹外还提供了几款 demo:1. non-image.htm...阅读全文
    posted @ 2014-10-28 17:19 dee0912 阅读(9) 评论(0) 编辑
    2014年10月27日    #
    PHP fwrite() 函数与 file_put_contents() 函数的比较
    摘要: 两个 PHP 函数都可以把字符串保存到文件中,fwrite() 函数的格式是:int fwrite ( resource handle , string string [ , int length] )它只能写入字符串。file_put_contents() 函数的格式是:int file_put_...阅读全文
    posted @ 2014-10-27 22:48 dee0912 阅读(5) 评论(0) 编辑
    jQuery 图片剪裁插件使用之 imgAreaSelect
    摘要: 插件主页:http://odyniec.net/projects/imgareaselect/官方网站上说明支持的浏览器:The plugin works in all major browsers, including Firefox 2+, Opera 9.5+, Google Chrome, ...阅读全文
    posted @ 2014-10-27 16:12 dee0912 阅读(9) 评论(0) 编辑
    2014年10月26日    #
    PHP 支持中文目录和文件的的遍历
    摘要: 在使用 readdir() 遍历指定目录时,使中文目录和文件名都正常显示需要使用 iconv() 进行文件编码转换: 1 ';13 echo '目录'.$dirname.'下面的内容';14 echo '';15 echo '文件名文件大小文件类型修改时间';16 17 while($file = ...阅读全文
    posted @ 2014-10-26 12:43 dee0912 阅读(7) 评论(0) 编辑
    View Code

    此时输出的字符是:

    ) ±à¼­ 

    把上面的代码进行文件编码转换:

    $fp = fopen('data.txt','r') or die("文件打开失败");
    fseek($fp,-10,SEEK_END);
    echo iconv('gb2312','utf-8',fread($fp,10))."<br>";

    输出:

    ) 编辑 

  • 相关阅读:
    ....
    CodeForces 375A(同余)
    POJ 2377 Bad Cowtractors (最小生成树)
    POJ 1258 AgriNet (最小生成树)
    HDU 1016 Prime Ring Problem(全排列)
    HDU 4460 Friend Chains(bfs)
    POJ 2236 Wireless Network(并查集)
    POJ 2100 Graveyard Design(尺取)
    POJ 2110 Mountain Walking(二分/bfs)
    CodeForces 1059B Forgery(模拟)
  • 原文地址:https://www.cnblogs.com/dee0912/p/4051853.html
Copyright © 2011-2022 走看看