PHP 支援中文目錄和檔案的的遍曆

來源:互聯網
上載者:User

標籤:style   blog   color   io   os   ar   使用   strong   sp   

在使用 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個

PHP 支援中文目錄和檔案的的遍曆

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.