Home  >  Article  >  php教程  >  PHP遍历某个目录下的文件

PHP遍历某个目录下的文件

PHP中文网
PHP中文网Original
2016-05-25 17:09:031085browse

PHP遍历某个目录下的文件

PHP代码

<?php
   $num=0;    //用来记录目录下的文件个数
   $dirname=&#39;LAMP&#39;; //要遍历的目录名字
   $dir_handle=opendir($dirname);
 
   echo &#39;<table border="1" align="center" width="960px" cellspacing="0" cellpadding="0">&#39;;
   echo &#39;<caption><h2>目录&#39;.$dirname.&#39;下面的内容</h2></caption>&#39;;
   echo &#39;<tr align="left" bgcolor="#cccccc">&#39;;
   echo &#39;<th>文件名</th><th>文件大小</th><th>文件类型</th><th>修改时间</th></tr>&#39;;
   while($file=readdir($dir_handle))
   {
     if($file!="."&&$file!="..")
     {
        $dirFile=$dirname."/".$file;
        if($num++%2==0)    //隔行换色
            $bgcolor="#ffffff";
        else
            $bgcolor="#cccccc";
        echo &#39;<tr bgcolor=&#39;.$bgcolor.&#39;>&#39;;
        echo &#39;<td>&#39;.$file.&#39;</td>&#39;;
        echo &#39;<td>&#39;.filesize($dirFile).&#39;</td>&#39;;
        echo &#39;<td>&#39;.filetype($dirFile).&#39;</td>&#39;;
        echo &#39;<td>&#39;.date("Y/n/t",filemtime($dirFile)).&#39;</td>&#39;;
        echo &#39;</tr>&#39;;
     }
   }
   echo &#39;</table>&#39;;
   closedir($dir_handle);
   echo &#39;在<b>&#39;.$dirname.&#39;</b>目录下的子目录和文件共有<b>&#39;.$num.&#39;</b>个&#39;;
?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn