«
一个php目录类

时间:2008-5-31    作者:Deri    分类: 分享


   <p>  <code><?<br />//目录操作基类<br />class FileDirectory {<br /> var $servermode;<br /> var $serverpath;  //web服务器目录<br /> var $pagepath;  //当前页目录<br /> var $path;    //当前目录<br /> var $ffblk;    //用于存储有关文件的信息<br /> function FileDirectory() {<br />  set_time_limit(0);  //设置网页运行时间,0不限<br />  $this->serverpath = $GLOBALS[DOCUMENT_ROOT]."/";<br />  $this->path = $this->pagepath = dirname(eregi_replace("//","/",$GLOBALS[SCRIPT_FILENAME]))."/";<br />  if(eregi("Win32",getenv("SERVER_SOFTWARE")))<br />   $this->servermode = "WIN32";<br /> }<br /> function first_dir() {<br />  return dirname(eregi_replace("//","/",$GLOBALS[SCRIPT_FILENAME]));<br /> }<br /> //获取文件信息<br /> function file_info($filename) {<br />  $ar[name] = $filename;<br />  $ar[type] = filetype($filename);<br />  $ar[read] = is_readable($filename);<br />  $ar[write] = is_writeable($filename);<br />  $ar[exec] = is_executable($filename);<br />  $ar[time] = date("Y-m-d H:i:s",filemtime($filename));<br />  $ar[size] = filesize($filename);<br />  $ar[style] = ($ar[type]=="dir"?"d":"-")<br />       .($ar[read]?"r":"-")<br />       .($ar[write]?"w":"-")<br />       .($ar[exec]?"x":"-");<br />  return $ar;<br /> }<br /> function format_path($path){<br />  $tar = split("/",$path);<br />  $sar = split("/",$this->path);<br />  $t = count($tar);<br />  $s = count($sar);<br />  if($tar[$t-1] == "") $t--;<br />  if($sar[$s-1] == "") $s--;<br />  $j = 0;<br />  while($tar[$j] == "..") {<br />   $j++;<br />   $s--;<br />  }<br />  $p = "";<br />  for($i=0;$i<$s;$i++)<br />   $p .= $sar[$i]."/";<br />  for($i=$j;$i<$t;$i++)<br />   if($tar[$i] != ".")<br />    $p .= $tar[$i]."/";<br />  $this->path = $p;<br /> }<br /> //获取目录信息到数组,成功返回时$this->path为目录的全路径<br /> function array_dir($pathname=".") {<br />  $old = $this->path;<br />  if($this->servermode == "WIN32")<br />   $path = str_replace("\\","/",$pathname);<br />  else<br />   $path = $pathname;<br />  $this->format_path($path);<br />  if(! ($handle = @opendir($path))) {<br />   $path = dirname($pathname);<br />   $handle = opendir($path);<br />  }<br />  if(@chdir($this->path)) {<br />   while ($file = readdir($handle)) {<br />    $ar[] = $this->file_info($file);<br />   }<br />  }else<br />   $this->path = $old;<br />  closedir($handle);<br />  return $ar;<br /> }<br />}  //FileDirectory定义结束<br />?><br /><?<br />//目录对话框<br />class OpenFileDialog extends FileDirectory {<br /> var $filter = array("*.*");<br /> function Execute($path,$statpath) {<br />  if($path != "") {<br />   chdir($statpath);<br />   $this->path = $statpath;<br />   $ar = $this->array_dir($path);<br />  }else<br />   $ar = $this->array_dir(".");<br />  array_multisort($ar);<br />echo "<br /><style><br />td{font-size:9pt;}<br />select{font-size:9pt;}<br />#box{border:3px outset #ffffff}<br /></style><br /><form action=";<br />echo $GLOBALS[PHP_SELF];<br />echo " method=POST><br /><table bgcolor=#cccccc cellspacing=0 cellpadding=0><br /><tr><td><br /><table border=0 id=box><br /><tr><td><br />";<br />echo "当前路径 ".$this->path."<br>\n";<br />echo "<input type=hidden name=statpath value=\"".$this->path."\">\n";<br />echo "<select name=dirlist size=6 style=\"width:100px\" onChange=\"this.form.submit()\">\n";<br />for($i=0;$i<count($ar);$i++)<br /> if($ar[$i][type] == "dir")<br />  if($ar[$i][name] == ".")<br />   echo "<option selected>".$ar[$i][name]."\n";<br />  else<br />   echo "<option>".$ar[$i][name]."\n";<br />echo "</select> \n";<br />echo "<select size=6 style=\"width:100px\">\n";<br />for($i=0;$i<count($ar);$i++)<br /> if($ar[$i][type] == "file")<br />  echo "<option>".$ar[$i][name]."\n";<br />echo "<br /></select><br /></td></tr><br /></table><br /></td></tr><br /></table><br /></form><br />";<br /> }<br />}  //OpenFileDialog<br />?><br /><?<br />//测试<br />$dir = new OpenFileDialog();<br />echo "服务器类型 ".$dir->servermode."<br>";<br />echo "服务器路径 ".$dir->serverpath."<br>";<br />echo "当前页路径 ".$dir->pagepath."<br>";<br />echo "当前路径 ".$dir->path."<br>";<br />$dir->Execute($dirlist,$statpath);<br />?></code></p>