欢迎光临
我们一直在努力

一个目录类-PHP教程,PHP应用

建站超值云服务器,限时71元/月

<?
//目录操作基类
class filedirectory {
  var $servermode;
  var $serverpath;    //web服务器目录
  var $pagepath;    //当前页目录
  var $path;        //当前目录
  var $ffblk;        //用于存储有关文件的信息
  function filedirectory() {
    set_time_limit(0);    //设置网页运行时间,0不限
    $this->serverpath = $globals[document_root]."/";
    $this->path = $this->pagepath = dirname(eregi_replace("//","/",$globals[script_filename]))."/";
    if(eregi("win32",getenv("server_software")))
      $this->servermode = "win32";
  }
  function first_dir() {
    return dirname(eregi_replace("//","/",$globals[script_filename]));
  }
  //获取文件信息
  function file_info($filename) {
    $ar[name] = $filename;
    $ar[type] = filetype($filename);
    $ar[read] = is_readable($filename);
    $ar[write] = is_writeable($filename);
    $ar[exec] = is_executable($filename);
    $ar[time] = date("y-m-d h:i:s",filemtime($filename));
    $ar[size] = filesize($filename);
    $ar[style] = ($ar[type]=="dir"?"d":"-")
              .($ar[read]?"r":"-")
              .($ar[write]?"w":"-")
              .($ar[exec]?"x":"-");
    return $ar;
  }

  function format_path($path){
    $tar = split("/",$path);
    $sar = split("/",$this->path);
    $t = count($tar);
    $s = count($sar);
    if($tar[$t-1] == "") $t–;
    if($sar[$s-1] == "") $s–;
    $j = 0;
    while($tar[$j] == "..") {
      $j++;
      $s–;
    }
    $p = "";
    for($i=0;$i<$s;$i++)
      $p .= $sar[$i]."/";
    for($i=$j;$i<$t;$i++)
      if($tar[$i] != ".")
        $p .= $tar[$i]."/";
    $this->path = $p;
  }
  //获取目录信息到数组,成功返回时$this->path为目录的全路径
  function array_dir($pathname=".") {
    $old = $this->path;
    if($this->servermode == "win32")
      $path = str_replace("\\","/",$pathname);
    else
      $path = $pathname;
    $this->format_path($path);
    if(! ($handle = @opendir($path))) {
      $path = dirname($pathname);
      $handle = opendir($path);
    }
    if(@chdir($this->path)) {
      while ($file = readdir($handle)) {
        $ar[] = $this->file_info($file);
      }
    }else
      $this->path = $old;
    closedir($handle);
    return $ar;
  }
}    //filedirectory定义结束

?>

<?
//目录对话框
class openfiledialog extends filedirectory {
  var $filter = array("*.*");
  function execute($path,$statpath) {
    if($path != "") {
      chdir($statpath);
      $this->path = $statpath;
      $ar = $this->array_dir($path);
    }else
      $ar = $this->array_dir(".");
    array_multisort($ar);
echo "
<style>
td{font-size:9pt;}
select{font-size:9pt;}
#box{border:3px outset #ffffff}
</style>
<form action=";
echo $globals[php_self];
echo " method=post>
<table bgcolor=#cccccc cellspacing=0 cellpadding=0>
<tr><td>
<table border=0 id=box>
<tr><td>
";
echo "当前路径 ".$this->path."<br>\n";
echo "<input type=hidden name=statpath value=\"".$this->path."\">\n";

echo "<select name=dirlist size=6 style=\"width:100px\" onchange=\"this.form.submit()\">\n";
for($i=0;$i<count($ar);$i++)
  if($ar[$i][type] == "dir")
    if($ar[$i][name] == ".")
      echo "<option selected>".$ar[$i][name]."\n";
    else
      echo "<option>".$ar[$i][name]."\n";
echo "</select>  \n";
echo "<select size=6 style=\"width:100px\">\n";
for($i=0;$i<count($ar);$i++)
  if($ar[$i][type] == "file")
    echo "<option>".$ar[$i][name]."\n";
echo "
</select>
</td></tr>
</table>
</td></tr>
</table>
</form>
";
  }
}    //openfiledialog
?>

<?
//测试

$dir = new openfiledialog();
echo "服务器类型 ".$dir->servermode."<br>";
echo "服务器路径 ".$dir->serverpath."<br>";
echo "当前页路径 ".$dir->pagepath."<br>";
echo "当前路径 ".$dir->path."<br>";
$dir->execute($dirlist,$statpath);
?>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 一个目录类-PHP教程,PHP应用
分享到: 更多 (0)

相关推荐

  • 暂无文章