search
Homephp教程PHP源码PHP遍历某个目录下的文件

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software