已掉线,重新登录

首页 > 绿虎论坛 > 历史版块 > 编程 > PHP > 源码

标题: 这程序怎么写?

作者: @Ta

时间: 2013-06-27

点击: 1180

遍历一个目录下的全部子目录与文件,并生成它们的访问链接。
这该怎么者写?

[隐藏样式|查看源码]


『回复列表(2|隐藏机器人聊天)』

1. @老虎会游泳 代码中的href里的代码被替换了!
很久以前写的,不知道该能不能用,,,
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html;
charset=utf-8" />
<title>洋子的文件-分享</title>
<style type="text/css">
.bg{background-color:#009BCE;}
.all{background-color:#E8E8E8;}
</style>
</head>
<body style="font-size:30px" >
<div class="all">
<?php
$path='./';
$self=basename($PHP_SELF);
if($_GET['path']!='') $path=$_GET['path'];
else $_GET['path']='./';
#下面这段代码来源网络
?>



<?php
/**
 * Goofy 2011-11-30
 * getDir()去文件夹列表,getFile()去对应文件夹下面的文件列表,二者的区别在于判断有没有“.”后缀的文件,其他都一样
 */
//获取文件目录列表
function getDir($dir) {

 if (false != ($handle = opendir ( $dir ))) {
  $i=0;
  while ( false !== ($file = readdir ( $handle )) ) {
   //去掉"“.”、“..”以及带“.xxx”后缀的文件
   if ($file != "." && $file != ".."&&!strpos($file,".")) {
    

if(($i%2)==0) echo '<div class="bg" >'.'&nbsp;&nbsp;&nbsp;&nbsp;<a href="'.$self.'?path='.$_GET['path'].'/'.$file.'">'.$file.'</a></div>';
  else echo '&nbsp;&nbsp;&nbsp;&nbsp;<a href="'.$self.'?path='.$_GET['path'].'/'.$file.'">'.$file.'</a>';
  $i++;
   }
  }
  //关闭句柄
  closedir ( $handle );
 }
 return $dirArray;
}
//获取文件列表
function getFile($dir) {
 if (false != ($handle = opendir ( $dir ))) {
  $i=0;
  while ( false !== ($file = readdir ( $handle )) ) {
   //去掉"“.”、“..”以及带“.xxx”后缀的文件
   if ($file != "." && $file != "LOST.DIR" && $file != ".."&&strpos($file,".")) {
   $size=filesize($_GET['path'].'/'.$file);
if($size>=1024 && $size<1048576)
{
$size/=1024;
$b='KB';
}
else if($size>=1048576)
{
$size/=1048576;
$b='MB';
}
else $b='B';
$size=round($size, 2);

if($i%2==0) echo '<div class="bg"><font color=#FF0000>'.$size.$b.'</font>&nbsp;&nbsp;&nbsp;&nbsp;<a href="./'.$_GET['path'].'/'.$file.'">'.$file.'</a></div>';
else echo '<font color=#FF0000>'.$size.$b.'</font>&nbsp;&nbsp;&nbsp;&nbsp;<a href="./'.$_GET['path'].'/'.$file.'">'.$file.'</a>';

    if($i==255){
     break;
    }
    $i++;
   }
  }
  //关闭句柄
  closedir ( $handle );
 }
}
//调用方法getDir("./dir")……
?>







=====目录列表=====
<br/><a href="<?php
echo $self.'?path='.dirname($_GET['path']).'"><font color=FF0000>↑向上↑</font></a>&nbsp;&nbsp;';
?>当前目录:
<?php
echo $path;
getDir($path);

?>
<br/><br/>=====文件列表=====<br/>
<?php
getFile($path);
?>


<br/><br/>=====BY:洋子=====
</div>
</body>
</html>
(/@Ta/2013-06-27 17:21//)

2. @ 谢咯!待会去试试
(/@Ta/2013-06-27 17:36//)

回复需要登录

10月10日 20:33 星期五

本站由hu60wap6驱动

备案号: 京ICP备18041936号-1