<?php
//
//取得一个目录下的所有文件名字列表
//
//path:目录
//orderBy:以什么排序 "none"自然 "times"时间 "size"大小 "name"名字
//seq:升序还是降序 "asc"升 "des"降
class files {
var $name = "";
var $size = 0;
var $times = 0;
}
class listfiles {
var $num = 0; //文件个数
var $filel = ""; //保存文件结构的数组
var $size = 0; //所有文件的大小
var $path = ""; //文件所在目录
var $orderBy = ""; //按什么排序
var $seq = ""; //升序还是降序
function listfiles ($path, $orderBy, $seq) {
$this->path = $path;
$this->orderBy = $orderBy;
$this->seq = $seq;
}
function getlist () {
if (substr ($this->path, strlen ($this->path) - 1, 1) == "/" &&
substr ($this->path, strlen ($this->path) - 1, 1) == "\\") {
$this->path = substr ($this->path, 0, strlen ($this->path) -1);
}
if (!file_exists ($this->path)) return $GLOBALS["DIR_NOT_EXIST"];
else if (!is_dir ($this->path)) return $GLOBALS["IS_NOT_DIR"];
$handle = opendir($this->path);
while ($entry = readdir ($handle)) {
if (!is_file ($this->path."/".$entry)) continue;
if ($entry[0] == ".") continue;
$this->filel[$this->num] = new files ();
$this->filel[$this->num]->name = $entry;
$this->filel[$this->num]->size = filesize ($this->path."/".$entry);
$this->filel[$this->num]->times = filemtime ($this->path."/".$entry);
$this->size += $this->filel[$this->num]->size;
$this->num ++;
}
closedir ($handle);
}
function order () {
if ($this->orderBy == "none") return;
for ($i = 0; $i <= $this->num - 2; $i++)
for ($j = $i + 1; $j <= $this->num -1; $j++) {
if ($this->orderBy == "name") {
$compStr1 = $this->filel[$i]->name;
$compStr2 = $this->filel[$j]->name;
} else if ($this->orderBy == "times") {
$compStr1 = $this->filel[$i]->times;
$compStr2 = $this->filel[$j]->times;
} else if ($this->orderBy == "size") {
$compStr1 = $this->filel[$i]->size;
$compStr2 = $this->filel[$j]->size;
}
if ($this->seq == "asc") {
if ($compStr1 > $compStr2) {
$tempStr = $this->filel[$i]->name;
$this->filel[$i]->name = $this->filel[$j]->name;
$this->filel[$j]->name = $tempStr;
$tempInt = $this->filel[$i]->size;
$this->filel[$i]->size = $this->filel[$j]->size;
$this->filel[$j]->size = $tempInt;
$tempInt = $this->filel[$i]->times;
$this->filel[$i]->times = $this->filel[$j]->times;
$this->filel[$j]->times = $tempInt;
}
} else if ($this->seq == "des") {
if ($compStr1 < $compStr2) {
$tempStr = $this->filel[$i]->name;
$this->filel[$i]->name = $this->filel[$j]->name;
$this->filel[$j]->name = $tempStr;
$tempInt = $this->filel[$i]->size;
$this->filel[$i]->size = $this->filel[$j]->size;
$this->filel[$j]->size = $tempInt;
$tempInt = $this->filel[$i]->times;
$this->filel[$i]->times = $this->filel[$j]->times;
$this->filel[$j]->times = $tempInt;
}
}
}
}
function dspTest () {
echo "<table>\n";
for ($i = 0; $i < $this->num; $i ++) {
echo "<tr><td>".$this->filel[$i]->name."</td>";
echo "<td>".$this->filel[$i]->size."</td>";
echo "<td>".$this->filel[$i]->times."</td></tr>\n";
}
echo "</table>\n";
echo "共 $this->num 个文件,总长度 $this->size bytes<br>";
}
}
?>
|