PHP 读取目录,删除

本文章提供一款php目录管理程序,他可以对目录下的文件,文件夹,等各种文件进行管理删除操作,可以支持无限级目录的管理,代码如下:

  1. <?php
  2. include("class.php");
  3. $path = $_get['path'];
  4. if($path == ""){
  5. $path = "dir";
  6. }else{
  7. if(!strstr($path,"dir")){
  8. $path = "dir/".$path;
  9. }else{
  10. $path = $path;
  11. }
  12. }
  13. $newdir = new dirver();
  14. $newdir -> setpath($path);
  15. $newdir -> dirdata();
  16. $files = $newdir -> getfiles();
  17. $dirs = $newdir -> getdirs();
  18. //print_r($files);
  19. //print_r($dirs);
  20. echo('<link href="style.css" rel="stylesheet" type="text/css" />');
  21. $max = 3;
  22. $j = 0;
  23. if(count($dirs) == 2){
  24. echo'<table width="800" cellpadding="5" cellspacing="5"><tr>';
  25. print("<td width='33%'><img src='type/dir.png'/>");
  26. print('<a href="test.php?path='.$path."/".$dirs[0].'">');
  27. print($dirs[0]);
  28. print('</a>');
  29. print("</td>");
  30. print("<td width='33%'><img src='type/dir.png'/>");
  31. print('<a href="test.php?path='.$path."/".$dirs[1].'">');
  32. print($dirs[1]);
  33. print('</a>');
  34. print("</td>");
  35. print("<td width='33%'>");
  36. print("&nbsp;&nbsp;");
  37. print("</td>");
  38. echo '</tr></table>';
  39. }else{
  40. echo'<table width="800" cellpadding="5" cellspacing="5"><tr>';
  41. while($j <= (count($dirs) -1)){
  42. print("<td ><img src='type/dir.png'/>");
  43. print('<a href="test.php?path='.$path."/".$dirs[$j].'">');
  44. print($dirs[$j]);
  45. print('</a>');
  46. print("</td>");
  47. if(($j + 1) % $max == 0){
  48. echo '</tr>';
  49. if(($j + 1) != count($dirs)){
  50. echo '<tr>';
  51. }
  52. }
  53. $j++;
  54. }
  55. echo '</tr></table>';
  56. }
  57. $i = 0;
  58. if(count($files) == 2){
  59. echo'<table width="800" cellpadding="5" cellspacing="5"><tr>';
  60. print("<td width='33%'><img src='type/".$newdir -> getfiletype($files[0]).".png'/>&nbsp;");
  61. print($newdir -> change2line($files[0]));
  62. print("</td>");
  63. print("<td width='33%'><img src='type/".$newdir -> getfiletype($files[1]).".png'/>&nbsp;");
  64. print($newdir -> change2line($files[1]));
  65. print("</td>");
  66. print("<td width='33%'>");
  67. print("&nbsp;&nbsp;");
  68. print("</td>");
  69. echo '</tr></table>';
  70. }else{
  71. echo'<table width="800" cellpadding="5" cellspacing="5"><tr>';
  72. while($i <= (count($files) -1)){
  73. print("<td width=100><img src='type/".$newdir -> getfiletype($files[$i]).".png'/>&nbsp;");
  74. print($newdir -> change2line($files[$i]));
  75. print("</td>");//开源代码phpfensi.com
  76. if(($i + 1) % 3 == 0){
  77. echo '</tr>';
  78. if(($i + 1) != count($files)){
  79. echo '<tr>';
  80. }
  81. }
  82. $i++;
  83. }
  84. echo '</tr></table>';
  85. }
  86. ?>

class.php,代码如下:

  1. <?php
  2. class for php4.x
  3. class dirver{
  4. /class var/
  5. var $path;
  6. var $flies;
  7. var $dirs;
  8. /
  9. function dirver(){
  10. $this -> path = "";
  11. $this -> files = array();
  12. $this -> dirs = array();
  13. }
  14. function dirdata(){
  15. if(isset($this -> path)){
  16. $handle = dir($this -> path);
  17. while(false !== ($data = $handle -> read())){
  18. if(is_dir($this -> connectname($this -> path,$data)) && $data != "." && $data != ".."){
  19. $this -> dirs[] = $data;
  20. continue;
  21. }
  22. if($data != "." && $data != ".." && is_file($this -> connectname($this -> path,$data))){
  23. $this -> files[] = $data;
  24. continue;
  25. }
  26. }
  27. $handle -> close();
  28. }else{
  29. return false;
  30. }
  31. }
  32. function setpath($src){
  33. if($src != ""){
  34. $this -> path = $src;
  35. }else{
  36. return false;
  37. }
  38. }
  39. /
  40. function connectname($path,$name){
  41. return $path."/".$name;
  42. }
  43. /
  44. function change2line($name){
  45. $basename = explode(".",$name);
  46. $basename = $basename[0];
  47. $tmp = $this -> path."/".$name;
  48. $tmp = '<a href="'.$tmp.'" target="_blank">'.$basename.'</a>';
  49. return $tmp;
  50. }
  51. function getfiletype($file){
  52. if($file != ""){
  53. $tmp = explode(".",$file);
  54. $type = $tmp[count($tmp)-1];
  55. return $type;
  56. }
  57. }
  58. function getfiles(){
  59. return $this -> files;
  60. }
  61. function getdirs(){
  62. return $this -> dirs;
  63. }
  64. //
  65. }
  66. ?>