Home > Backend Development > PHP Tutorial > PHP traverses folders and implements editing and deletion code

PHP traverses folders and implements editing and deletion code

WBOY
Release: 2016-07-25 08:55:55
Original
2044 people have browsed it
  1. /*
  2. Traverse folders; Functions: (a) Deleteable files (b) Editable text, web page files (c) Deleteable folders, provided the folder is empty ( d) Files, folders, and folder names can be created;
  3. */
  4. $path=$DOCUMENT_ROOT;
  5. $path=str_replace("/","\",$path);
  6. //Specify the folder
  7. $ path=$path."\";
  8. if($_GET['folder'])
  9. {
  10. $path.=$_GET['folder']."\";
  11. }
  12. //The path of this page, pass it below It will be used when returning;
  13. $url=$_SERVER['PHP_SELF'];
  14. //If it is a folder, a link will be added;
  15. function folder($path,$str)
  16. {
  17. if(filetype($path .$str)=="dir")
  18. {
  19. return "$str}else{
  20. return $str;
  21. }
  22. }
  23. switch($_GET['action'])
  24. {
  25. case "del";
  26. if($_GET['type']=="file" )
  27. {
  28. unlink($_GET['path']);
  29. }else{
  30. rmdir($_GET['path']);
  31. }
  32. echo "";
  33. break;
  34. case "edit";
  35. if($_GET['type']== "file")
  36. {
  37. $file=fopen($_GET['path'],"r");
  38. while(!feof($file))
  39. {
  40. $result.=fgets($file,9999);
  41. }
  42. fclose($file);
  43. echo '
  44. < ;td height="25" bgcolor="#99CC00">>> Edit file > Return< /a>
  45. ->File name:'.$ _GET['path'].'
  46. ';
  47. }else{
  48. echo '
  49. >> New file > < ;a href="'.$url.'">Return
    ->Folder rename:
  50. ' ;
  51. }
  52. break;
  53. case "editsave";
  54. $file=fopen($_GET['path'],"w");
  55. fwrite($file,$_POST['textarea']);
  56. fclose($file);
  57. echo "";
  58. break;
  59. case "addfile";
  60. echo '
  61. >> 新建文件 > 返回
    ->文件名:
  62. ';
  63. break;
  64. case "filesave";
  65. //包含点则建立文件,否则建立文件夹
  66. if(strpos($_POST['filename'],"."))
  67. {
  68. $file=fopen($_POST['filename'],"w");
  69. fwrite($file,$_POST['textarea']);
  70. fclose($file);
  71. }else{
  72. //文件夹若存在则退出,不存在则建立!
  73. if(file_exists($_POST['filename']))
  74. {
  75. exit;
  76. }else{
  77. mkdir($_POST['filename']);
  78. }
  79. }
  80. echo "";
  81. break;
  82. case "dir_rename";
  83. rename($_GET['path'],$_POST['filename']);
  84. echo "";
  85. break;
  86. default:
  87. $s=explode("n",trim(`dir/b/o:gn $path`));
  88. echo '
  89. ';
  90. foreach($s as $value)
  91. {
  92. echo '
  93. ';
  94. }
  95. echo "
  96. >> 文件管理 > 建立文件 > 返回
    文件/文件夹 文件属性 文件大小 操作
    '.folder($path,$value).' '.filetype($path.$value).' '.round(filesize($path.$value)/1024).'kb 编辑 | 删除
    ";
  97. break;
  98. }
  99. ?>
Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template