首頁 > php教程 > php手册 > 主體

一个目录类

WBOY
發布: 2016-06-21 09:12:00
原創
1815 人瀏覽過

//目录操作基类
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      $p .= $sar[$i]."/";
    for($i=$j;$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 "








";
echo "当前路径 ".$this->path."
\n";
echo "path."\">\n";

echo "  \n";
echo "



";
  }
}    //OpenFileDialog
?>


//测试

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



相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板