前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >原生php实现遍历目录信息

原生php实现遍历目录信息

作者头像
贵哥的编程之路
发布2022-05-06 12:42:10
1.3K0
发布2022-05-06 12:42:10
举报
文章被收录于专栏:用户7873631的专栏
代码语言:javascript
复制
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> 遍历目录信息 </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="dz5362">
  <meta name="Keywords" content="dz5362">
  <meta name="Description" content="no-description">
  <meta http-equiv="Content-type" content="text/html;charset=GB2312">
   <link rel='stylesheet' type='text/css' href='style.css'>
 </head>

 <body>
 <div id='header'>遍历指定目录信息</div>
 <div id='content'>
  <form method="post" action="cyg.php" id='myForm'>
  <center>请输入目录路径</center>
  <input type='text' name='path' size='25'><br>
  <input type="submit" value='查看' class='input'>
  </form>
  </div>
  <div id='footer'>cyg 2012</div>
 </body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
   <style>
  table{
  border-collapse:collapse;
  border:1px solid #ccc;
  }
  table td{
  border:1px solid #ccc;
  line-height:22px;
  background:#eee;
  }
  </style>
 </head>

 <body>
 <?php
 if($_POST['path']){
  $path_start = $_POST['path']; //获取绝对路径
  $path = opendir($path_start); //打开目录
  ?>
 <center>目录信息如下表所示</center>
  <table border='0' cellspacing='0' cellpadding='0' width='450' align='center'>
  <tr align="left" bgcolor="#cccccc">
    <th>文件名</th><th>大小</th><th>类型</th><th>修改时间</th>
  </tr>
  <?php
    $str = '';
    while($file=readdir($path)){//读取某个目录下的信息
    $dir = $path_start."/".$file;     //获取完整的路径和文件名称
    $str .=  '<tr align=\'center\'>';             
    $str .= '<td>'.$file.'</td>';                     
        $str .= '<td>'.ceil((filesize($dir))/1024).'kb</td>';//获取目录下某个文件的大小
        $str .= '<td>'.(filetype($dir)=='dir'?'文件夹':'文件').'</td>';//看类型是文件还是文件夹
        $str .= '<td>'.date("Y/n/t",filemtime($dir)).'</td>';//查看目录的日期
        $str .= '</tr>';  
  }
  echo $str;
  ?>
  </table>
  <?php
 }
 ?>
</body>
</html>
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-03-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档