【慕课网】WEB在线文件管理器

电脑中对文件进行相关操作:
新建,重命名,得到相关信息,名字类型,创建时间,访问权限,可读可执行,
复制,删除,双击,创建文件夹,编辑,

文件相关操作:
  • 创建文件
  • 判断文件的权限
  • 文件的大小
  • 文件创建时间、修改时间、访问时间
  • 查看文件的内容
  • 修改文件的内容
  • 删除文件
  • 重命名文件
  • 复制、剪切
  • 上传和下载
文件夹相关操作
  • 新建文件夹
  • 判断文件夹权限
  • 文件夹大小
  • 文件夹的创建、修改、访问时间
  • 查看文件内容
  • 重命名
  • 复制、剪切
  • 下载

获得首层信息


需要得到目录中的内容,
通过遍历目录实现

  1. <?php
  2. function readDirectory ($path){
  3. $handle = opendir($path);
  4. while (($item = readdir($handle))!== false){
  5. // .和..这两个特殊的目录
  6. // 做不了操作
  7. if ($item != "." && $item != ".."){
  8. if (is_file ($path."/".$item)){
  9. $arr['file'][] = $item;
  10. }else if(is_dir ($path."/".$item)) {
  11. $arr['dir'][] = $item;
  12. }
  13. }
  14. }
  15. closedir($handle);
  16. return $arr;
  17. }
  18. $path = "file";
  19. print_r (readDirectory($path));
  20. ?>

列表显示文件大小

index.html
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  5. <title>Insert title here</title>
  6. <link rel="stylesheet" href="cikonss.css" />
  7. <style type="text/css">
  8. body,p,div,ul,ol,table,dl,dd,dt{
  9. margin:0;
  10. padding: 0;
  11. }
  12. a{
  13. text-decoration: none;
  14. }
  15. ul,li{
  16. list-style: none;
  17. float: left;
  18. }
  19. #top{
  20. width:100%;
  21. height:48px;
  22. margin:0 auto;
  23. background: #E2E2E2;
  24. }
  25. #navi a{
  26. display: block;
  27. width:48px;
  28. height: 48px;
  29. }
  30. #main{
  31. margin:0 auto;
  32. border:2px solid #ABCDEF;
  33. }
  34. .small{
  35. width:25px;
  36. height:25px;
  37. border:0;
  38. }
  39. </style>
  40. </head>
  41. <body>
  42. <div id="showDetail" style="display:none"><img src="" id="showImg" alt=""/></div>
  43. <h1>慕课网-在线文件管理器</h1>
  44. <div id="top">
  45. <ul id="navi">
  46. <li><a href="index.php" title="主目录"><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span class="icon-home"></span></span></a></li>
  47. <li><a href="#" onclick="show('createFile')" title="新建文件" ><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span class="icon-file"></span></span></a></li>
  48. <li><a href="#" onclick="show('createFolder')" title="新建文件夹"><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span class="icon-folder"></span></span></a></li>
  49. <li><a href="#" onclick="show('uploadFile')"title="上传文件"><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span class="icon-upload"></span></span></a></li>
  50. <li><a href="#" title="返回上级目录" onclick="goBack('<?php echo $back;?>')"><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span class="icon-arrowLeft"></span></span></a></li>
  51. </ul>
  52. </div>
  53. </body>
  54. </html>

显示文件名:

  1. <?php
  2. require_once 'dir.func.php';
  3. $path = "file";
  4. $info = readDirectory($path);
  5. // print_r ($info);
  6. ?>
  7. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  8. <html xmlns="http://www.w3.org/1999/xhtml">
  9. <head>
  10. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  11. <title>Insert title here</title>
  12. <link rel="stylesheet" href="cikonss.css" />
  13. <style type="text/css">
  14. body,p,div,ul,ol,table,dl,dd,dt{
  15. margin:0;
  16. padding: 0;
  17. }
  18. a{
  19. text-decoration: none;
  20. }
  21. ul,li{
  22. list-style: none;
  23. float: left;
  24. }
  25. #top{
  26. width:100%;
  27. height:48px;
  28. margin:0 auto;
  29. background: #E2E2E2;
  30. }
  31. #navi a{
  32. display: block;
  33. width:48px;
  34. height: 48px;
  35. }
  36. #main{
  37. margin:0 auto;
  38. border:2px solid #ABCDEF;
  39. }
  40. .small{
  41. width:25px;
  42. height:25px;
  43. border:0;
  44. }
  45. </style>
  46. </head>
  47. <body>
  48. <div id="showDetail" style="display:none"><img src="" id="showImg" alt=""/></div>
  49. <h1>慕课网-在线文件管理器</h1>
  50. <div id="top">
  51. <ul id="navi">
  52. <li><a href="index.php" title="主目录"><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span class="icon-home"></span></span></a></li>
  53. <li><a href="#" onclick="show('createFile')" title="新建文件" ><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span class="icon-file"></span></span></a></li>
  54. <li><a href="#" onclick="show('createFolder')" title="新建文件夹"><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span class="icon-folder"></span></span></a></li>
  55. <li><a href="#" onclick="show('uploadFile')"title="上传文件"><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span class="icon-upload"></span></span></a></li>
  56. <li><a href="#" title="返回上级目录" onclick="goBack('<?php echo $back;?>')"><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span class="icon-arrowLeft"></span></span></a></li>
  57. </ul>
  58. </div>
  59. <table width="100%" border="1" cellpadding="5" cellspacing="0" bgcolor="#ABCDEF" align="center" >
  60. <tr>
  61. <td>编号</td>
  62. <td>名称</td>
  63. <td>类型</td>
  64. <td>大小</td>
  65. <td>可读</td>
  66. <td>可写</td>
  67. <td>可执行</td>
  68. <td>创建时间</td>
  69. <td>修改时间</td>
  70. <td>访问时间</td>
  71. <td>操作</td>
  72. </tr>
  73. <?php
  74. if ($info['file']){
  75. $i = 1;
  76. foreach ($info['file'] as $val){
  77. ?>
  78. <tr>
  79. <td><?php echo $i ;?></td>
  80. <td><?php echo $val;?></td>
  81. <td><?php echo filetype($path."/".$val);?></td>
  82. </tr>
  83. <?php
  84. $i++;
  85. }
  86. }
  87. ?>
  88. </table>
  89. </body>
  90. </html>



修改为图片:
  1. <?php
  2. require_once 'dir.func.php';
  3. require_once 'file.func.php';
  4. $path = "file";
  5. $info = readDirectory($path);
  6. // print_r ($info);
  7. ?>
  8. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  9. <html xmlns="http://www.w3.org/1999/xhtml">
  10. <head>
  11. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  12. <title>Insert title here</title>
  13. <link rel="stylesheet" href="cikonss.css" />
  14. <style type="text/css">
  15. body,p,div,ul,ol,table,dl,dd,dt{
  16. margin:0;
  17. padding: 0;
  18. }
  19. a{
  20. text-decoration: none;
  21. }
  22. ul,li{
  23. list-style: none;
  24. float: left;
  25. }
  26. #top{
  27. width:100%;
  28. height:48px;
  29. margin:0 auto;
  30. background: #E2E2E2;
  31. }
  32. #navi a{
  33. display: block;
  34. width:48px;
  35. height: 48px;
  36. }
  37. #main{
  38. margin:0 auto;
  39. border:2px solid #ABCDEF;
  40. }
  41. .small{
  42. width:25px;
  43. height:25px;
  44. border:0;
  45. }
  46. </style>
  47. </head>
  48. <body>
  49. <div id="showDetail" style="display:none"><img src="" id="showImg" alt=""/></div>
  50. <h1>慕课网-在线文件管理器</h1>
  51. <div id="top">
  52. <ul id="navi">
  53. <li><a href="index.php" title="主目录"><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span class="icon-home"></span></span></a></li>
  54. <li><a href="#" onclick="show('createFile')" title="新建文件" ><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span class="icon-file"></span></span></a></li>
  55. <li><a href="#" onclick="show('createFolder')" title="新建文件夹"><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span class="icon-folder"></span></span></a></li>
  56. <li><a href="#" onclick="show('uploadFile')"title="上传文件"><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span class="icon-upload"></span></span></a></li>
  57. <li><a href="#" title="返回上级目录" onclick="goBack('<?php echo $back;?>')"><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span class="icon-arrowLeft"></span></span></a></li>
  58. </ul>
  59. </div>
  60. <table width="100%" border="1" cellpadding="5" cellspacing="0" bgcolor="#ABCDEF" align="center" >
  61. <tr>
  62. <td>编号</td>
  63. <td>名称</td>
  64. <td>类型</td>
  65. <td>大小</td>
  66. <td>可读</td>
  67. <td>可写</td>
  68. <td>可执行</td>
  69. <td>创建时间</td>
  70. <td>修改时间</td>
  71. <td>访问时间</td>
  72. <td>操作</td>
  73. </tr>
  74. <?php
  75. if ($info['file']){
  76. $i = 1;
  77. foreach ($info['file'] as $val){
  78. ?>
  79. <tr>
  80. <td><?php echo $i ;?></td>
  81. <td><?php echo $val;?></td>
  82. <td><?php $src = filetype($path."/".$val) == "file" ? "file_ico.png" : "folder_ico.png";?><img src = "images/<?php echo $src;?>" alt = "" title="文件"/></td>
  83. <td><?php echo transByte(filesize ($path."/".$val));?></td>
  84. </tr>
  85. <?php
  86. $i++;
  87. }
  88. }
  89. ?>
  90. </table>
  91. </body>
  92. </html>

构建file.func.php,转换字符大小
  1. <?php
  2. /* 转换字符大小。
  3. @parameter number $size
  4. @return number */
  5. function transByte ($size){
  6. // $size = 500000;
  7. $arr = array ("B","KB","MB","GB","TB","EB");
  8. $i = 0;
  9. while ($size >= 1024){
  10. $size/=1024;
  11. $i++;
  12. }
  13. // 取两位小数
  14. return round($size,2).$arr[$i];
  15. }
  16. ?>

可读:
  1. <td><?php $src = is_readable ($path ."/".$val) ? "correct.png" : "error.png";?><img class="small" src = "images/<?php echo $src;?>" alt = ""/></td>

创建时间:
  1. <td><?php echo filectime($p);?></td>

变为人认识的时间:
  1. <td><?php echo date( "Y-m-d H:i:s",filectime($p) ) ;?></td>
  2. <td><?php echo date( "Y-m-d H:i:s",filemtime($p) ) ;?></td>
  3. <td><?php echo date( "Y-m-d H:i:s",fileatime($p) ) ;?></td>

创建文件


文件名的合法性:不能包含/:*"等特殊字符。
检测当前目录下是否存在同名文件

  1. <div id="showDetail" style="display:none"><img src="" id="showImg" alt=""/></div>
  2. <h1>在线文件管理器</h1>
  3. <div id="top">
  4. <ul id="navi">
  5. <li>
  6. <a href="index.php" title="主目录">
  7. <span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square">
  8. <span class="icon-home">
  9. </span>
  10. </span>
  11. </a>
  12. </li>
  13. <li>
  14. <a href="#" onclick="show('createFile')" title="新建文件" >
  15. <span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square">
  16. <span class="icon-file"></span>
  17. </span>
  18. </a>
  19. </li>
  20. <li>
  21. <a href="#" onclick="show('createFolder')" title="新建文件夹">
  22. <span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square">
  23. <span class="icon-folder"></span>
  24. </span>
  25. </a>
  26. </li>
  27. <li>
  28. <a href="#" onclick="show('uploadFile')"title="上传文件">
  29. <span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square">
  30. <span class="icon-upload">
  31. </span>
  32. </span>
  33. </a>
  34. </li>
  35. <li>
  36. <a href="#" title="返回上级目录" onclick="goBack('
  37. <?php echo $back;?>')">
  38. <span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square">
  39. <span class="icon-arrowLeft">
  40. </span>
  41. </span>
  42. </a>
  43. </li>
  44. </ul>
  45. </div>
  1. <script type="text/javascript">
  2. function show(dis){
  3. document.getElementById(dis).style.display="block";
  4. }
  5. function delFile(filename,path){
  6. if(window.confirm("您确定要删除嘛?删除之后无法恢复哟!!!")){
  7. location.href="index.php?act=delFile&filename="+filename+"&path="+path;
  8. }
  9. }
  10. function delFolder(dirname,path){
  11. if(window.confirm("您确定要删除嘛?删除之后无法恢复哟!!!")){
  12. location.href="index.php?act=delFolder&dirname="+dirname+"&path="+path;
  13. }
  14. }
  15. function showDetail(t,filename){
  16. $("#showImg").attr("src",filename);
  17. $("#showDetail").dialog({
  18. height:"auto",
  19. width: "auto",
  20. position: {my: "center", at: "center", collision:"fit"},
  21. modal:false,//是否模式对话框
  22. draggable:true,//是否允许拖拽
  23. resizable:true,//是否允许拖动
  24. title:t,//对话框标题
  25. show:"slide",
  26. hide:"explode"
  27. });
  28. }
  29. function goBack($back){
  30. location.href="index.php?path="+$back;
  31. }
  32. </script>

把样式变成显示。

当触碰按钮的时候才显示。平时隐藏,点击的时候,调用show,把style变为block
  1. <tr id="createFolder" style="display:none;">
  1. <tr id="createFolder" style="display:none;">
  2. <td>请输入文件夹名称</td>
  3. <td >
  4. <input type="text" name="dirname" />
  5. <input type="hidden" name="path" value="<?php echo $path;?>"/>
  6. <input type="submit" name="act" value="创建文件夹"/>
  7. </td>
  8. </tr>

隐藏域:传路径
操作:
  1. <?php
  2. require_once 'dir.func.php';
  3. require_once 'file.func.php';
  4. $path = "file";
  5. $info = readDirectory($path);
  6. // print_r ($info);
  7. // $act = $_REQUEST['act'];
  8. $act=$_REQUEST['act'];
  9. $filename = $_REQUEST['filename'];
  10. $redirect = "index.php?path=${path}";
  11. if ($act == "创建文件"){
  12. // echo $path , "--";
  13. // echo $filename;
  14. // 创建文件
  15. $mes = createFile($path."/".$filename);
  16. // alertMes($mes,$redirect);
  17. }
  18. ?>
  1. function createFile ($filename){
  2. // 验证文件名的合法性
  3. $pattern = "/[\/,\*,<>,\?,\|]/";
  4. // $filename的形式为file/1.txt,所以需要进行处理
  5. if(!preg_match($pattern , basename($filename))){
  6. // 检测当前目录是否包含同名文件
  7. if(!file_exists($filename)){
  8. // 通过touch创建
  9. if(touch($filename)){
  10. return "文件创建成功";
  11. }else {
  12. return "文件创建失败";
  13. }
  14. }else {
  15. return "文件已存在,请重命名以后创建";
  16. }
  17. }else{
  18. return "非法文件名";
  19. }
  20. }
定义一个公共函数
告诉用户是否创建成功。
  1. /**
  2. * 提示操作信息的,并且跳转
  3. * @param string $mes
  4. * @param string $url
  5. */
  6. function alertMes($mes,$url){
  7. echo "<script type='text/javascript'>alert('{$mes}');location.href='{$url}';</script>";
  8. }
在index.php里面加上
  1. <?php
  2. require_once 'dir.func.php';
  3. require_once 'file.func.php';
  4. require_once 'common.func.php';
  5. $path = "file";
  6. $info = readDirectory($path);
  7. // print_r ($info);
  8. // $act = $_REQUEST['act'];
  9. $act=$_REQUEST['act'];
  10. $filename = $_REQUEST['filename'];
  11. // 跳转变量
  12. $redirect = "index.php?path=${path}";
  13. if ($act == "创建文件"){
  14. // echo $path , "--";
  15. // echo $filename;
  16. // 创建文件
  17. $mes = createFile($path."/".$filename);
  18. alertMes($mes,$redirect);
  19. }
  20. ?>

http://127.0.0.1/project/fileManager/index.php?path=file


查看文件内容


通过file_get_contents($filename)得到文件内容
通过highlight_string($string)或者highlight_file($filename)显示内容
使用PHP内置的语法高亮器所定义的颜色,打印输出或者返回输出或者返回语法高亮版本的PHP代码。
  1. <?php
  2. require_once 'dir.func.php';
  3. require_once 'file.func.php';
  4. require_once 'common.func.php';
  5. $path = "file";
  6. $info = readDirectory($path);
  7. // print_r ($info);
  8. // $act = $_REQUEST['act'];
  9. $act=$_REQUEST['act'];
  10. $filename = $_REQUEST['filename'];
  11. // 跳转变量
  12. $redirect = "index.php?path=${path}";
  13. if ($act == "创建文件"){
  14. // echo $path , "--";
  15. // echo $filename;
  16. // 创建文件
  17. $mes = createFile($path."/".$filename);
  18. alertMes($mes,$redirect);
  19. }elseif ($act == "showContent"){
  20. // 查看文件内容
  21. $content = file_get_contents($filename);
  22. // echo $content;
  23. // echo "<textarea readonly = 'readonly' cols='100' rows = '10'>{$content}</textarea>";
  24. // 高亮显示PHP代码
  25. // 第二个参数是true的话可以返回值
  26. $newContent = highlight_string($content , true);
  27. // 高亮显示文件中的PHP代码
  28. // highlight_file($filename);
  29. // 放到表格中
  30. $str = <<<EOF
  31. <table width = '100%' bgcolor = 'pink' cellpadding = '5' cellspacing = '0' >
  32. <tr>
  33. <td>{$newContent}</td>
  34. </tr>
  35. </table>
  36. EOF;
  37. echo $str;
  38. }
  39. ?>
如果文件本来就没东西,则需要加个判断。


mixed highlight_string ( string $str [, bool $return = false ] )

str

需要高亮的PHP代码,应当包含开始标签。

return

设置该参数为 TRUE 使函数返回高亮后的代码。


修改



  1. <?php
  2. require_once 'dir.func.php';
  3. require_once 'file.func.php';
  4. require_once 'common.func.php';
  5. $path = "file";
  6. $info = readDirectory($path);
  7. // print_r ($info);
  8. // $act = $_REQUEST['act'];
  9. $act=$_REQUEST['act'];
  10. $filename = $_REQUEST['filename'];
  11. // echo $filename;
  12. // 跳转变量
  13. $redirect = "index.php?path=${path}";
  14. if ($act == "createFile"){
  15. // echo $path , "--";
  16. // echo $filename;
  17. // 创建文件
  18. $mes = createFile($path."/".$filename);
  19. alertMes($mes,$redirect);
  20. }elseif ($act == "showContent"){
  21. // 查看文件内容
  22. $content = file_get_contents($filename);
  23. if (strlen($content)){
  24. // echo $content;
  25. // echo "<textarea readonly = 'readonly' cols='100' rows = '10'>{$content}</textarea>";
  26. // 高亮显示PHP代码
  27. // 第二个参数是true的话可以返回值
  28. $newContent = highlight_string($content , true);
  29. // 高亮显示文件中的PHP代码
  30. // highlight_file($filename);
  31. // 放到表格中
  32. $str = <<<EOF
  33. <table width = '100%' bgcolor = 'pink' cellpadding = '5' cellspacing = '0' >
  34. <tr>
  35. <td>{$newContent}</td>
  36. </tr>
  37. </table>
  38. EOF;
  39. echo $str;
  40. }else{
  41. alertMes("文件没有,请编辑",$redirect);
  42. }
  43. }elseif($act == "editContent"){
  44. // echo "编辑文件";
  45. // 此时返回的$filename已经包含了路径了。
  46. $content = file_get_contents($filename);
  47. // echo $content;
  48. $str=<<<EOF
  49. <form action='index.php?act=doEdit' method='post'>
  50. <textarea name='content' cols='190' rows='10'>{$content}</textarea><br/>
  51. <input type='hidden' name = 'filename' value = {$filename}>
  52. <input type = 'hidden' name='path' value = {$path}>
  53. <input type="submit" value="修改文件内容"/>
  54. </form>
  55. EOF;
  56. echo $str;
  57. }elseif($act=="doEdit"){
  58. //修改文件内容的操作
  59. $content=$_REQUEST['content'];
  60. echo $content;
  61. // 此时还没有传$filename
  62. if(file_put_contents($filename,$content)){
  63. $mes="文件修改成功";
  64. }else{
  65. $mes="文件修改失败";
  66. }
  67. alertMes($mes,$redirect);
  68. }
  69. ?>


查看图片

  • 如果是图片类型,点击查看直接显示图片
  • 如果不是图片,可以显示文件中的内容
  • 用到jQuery UI中的Dialog来显示图片

官网:jqueryui.com
通过文件的扩展名来获得图片类型。
引用:
  1. <script src="jquery-ui/js/jquery-1.10.2.js"></script>
  2. <script src="jquery-ui/js/jquery-ui-1.10.4.custom.js"></script>
  3. <script src="jquery-ui/js/jquery-ui-1.10.4.custom.min.js"></script>
  4. <link rel="stylesheet" href="jquery-ui/css/ui-lightness/jquery-ui-1.10.4.custom.css" type="text/css"/>

JS:设置
  1. function showDetail(t,filename){
  2. $("#showImg").attr("src",filename);
  3. $("#showDetail").dialog({
  4. height:"auto",
  5. width: "auto",
  6. position: {my: "center", at: "center", collision:"fit"},
  7. modal:false,//是否模式对话框
  8. draggable:true,//是否允许拖拽
  9. resizable:true,//是否允许拖动
  10. title:t,//对话框标题
  11. show:"slide",
  12. hide:"explode"
  13. });
  14. }

布局中:
  1. <div id="showDetail" style="display:none"><img src="" id="showImg" alt=""/></div>

  1. <td>
  2. <?php
  3. // 得到文件扩展名
  4. $ext = strtolower(end(explode(".",$val)));
  5. $imagExt = array ("gif","jpg","jpeg","png");
  6. if (in_array($ext , $imagExt)){
  7. ?>
  8. <a href="#" onclick="showDetail('<?php echo $val;?>','<?php echo $p;?>')">
  9. <img class="small" src="images/show.png" alt="" title="查看"/>
  10. </a>
  11. |
  12. <?php
  13. }else{
  14. ?>
  15. <a href="index.php?act=showContent&path=<?php echo $path;?>&filename=<?php echo $p;?>" >
  16. <img class="small" src="images/show.png" alt="" title="查看"/>
  17. </a>|
  18. <?php }?>

explode:以“.”作为分隔符
取最后一个,全部转换为小写。
如果$ext在$imagExt里面,
当点击的时候调用showDetail,传入文件名和路径。

重命名文件




  1. <a href="index.php?act=renameFile&path=<?php echo $path;?>&filename=<?php echo $p;?>">
  2. <img class="small" src="images/rename.png" alt="" title="重命名"/>
  3. </a>|
把renameFile传递过去,
使用PHP显示一个表格。
act= doRename
注意要同时把$filename也传递过去。
  1. }elseif($act == "renameFile"){
  2. // 重命名
  3. $str = <<<EOF
  4. <form action = "index.php?act=doRename" method = "post" >
  5. 请填写新文件名:<input type="text" name = "newname" placeholder="重命名"/></br>
  6. <input type='hidden' name='filename' value='{$filename}'/>
  7. <input type = 'submit' value='重命名'/>
  8. </form>
  9. EOF;
  10. echo $str;
  11. }elseif($act == "doRename"){
  12. // 实现重命名操作
  13. $newname = $_REQUEST['newname'];
  14. // echo $newname;
  15. $mes = renameFile($filename,$newname);
  16. alertMes ($mes , $redirect);
  17. }
通过文本框填写的内容获得$newname,调用renameFile函数,在file.func.php里面。
  1. /**
  2. * 重命名文件
  3. * @param string $oldname
  4. * @param string $newname
  5. * @return string
  6. */
  7. function renameFile($oldname,$newname){
  8. // echo $oldname,$newname;
  9. //验证文件名是否合法
  10. if(checkFilename($newname)){
  11. //检测当前目录下是否存在同名文件
  12. $path=dirname($oldname);
  13. if(!file_exists($path."/".$newname)){
  14. //进行重命名
  15. if(rename($oldname,$path."/".$newname)){
  16. return "重命名成功";
  17. }else{
  18. return "重命名失败";
  19. }
  20. }else{
  21. return "存在同名文件,请重新命名";
  22. }
  23. }else{
  24. return "非法文件名";
  25. }
  26. }
  27. /**
  28. *检测文件名是否合法
  29. * @param string $filename
  30. * @return boolean
  31. */
  32. function checkFilename($filename){
  33. $pattern = "/[\/,\*,<>,\?\|]/";
  34. if (preg_match ( $pattern, $filename )) {
  35. return false;
  36. }else{
  37. return true;
  38. }
  39. }


删除文件


  1. function delFile(filename,path){
  2. if(window.confirm("您确定要删除嘛?删除之后无法恢复哟!!!")){
  3. location.href="index.php?act=delFile&filename="+filename+"&path="+path;
  4. }
  5. }

传入一个操作delFile

  1. }elseif($act == "delFile"){
  2. // echo "文件删除";
  3. $mes = delFile($filename);
  4. alertMes($mes,$redirect);
  5. }elseif($act == "downFile"){
  6. // 完成下载操作
  7. $mes = downFile($filename);
  8. }


下载文件



  1. }elseif($act == "downFile"){
  2. // 完成下载操作
  3. $mes = downFile($filename);
  4. }

  1. function downFile($filename){
  2. header("content-disposition:attachment;filename=".basename($filename));
  3. header("content-length:".filesize($filename));
  4. readfile($filename);
  5. }


文件夹


查看文件夹

显示目录的布局文件。
  1. <!-- 读取目录的操作-->
  2. <?php
  3. if($info['dir']){
  4. $i=$i==null?1:$i;
  5. foreach($info['dir'] as $val){
  6. $p=$path."/".$val;
  7. echo $p;
  8. ?>
  9. <tr>
  10. <td><?php echo $i;?></td>
  11. <td><?php echo $val;?></td>
  12. <td><?php $src=filetype($p)=="file"?"file_ico.png":"folder_ico.png";?><img src="images/<?php echo $src;?>" alt="" title="文件"/></td>
  13. <td><?php $sum=0; echo transByte(dirSize($p));?></td>
  14. <td><?php $src=is_readable($p)?"correct.png":"error.png";?><img class="small" src="images/<?php echo $src;?>" alt=""/></td>
  15. <td><?php $src=is_writable($p)?"correct.png":"error.png";?><img class="small" src="images/<?php echo $src;?>" alt=""/></td>
  16. <td><?php $src=is_executable($p)?"correct.png":"error.png";?><img class="small" src="images/<?php echo $src;?>" alt=""/></td>
  17. <td><?php echo date("Y-m-d H:i:s",filectime($p));?></td>
  18. <td><?php echo date("Y-m-d H:i:s",filemtime($p));?></td>
  19. <td><?php echo date("Y-m-d H:i:s",fileatime($p));?></td>
  20. <td>
  21. <a href="index.php?path=<?php echo $p;?>" ><img class="small" src="images/show.png" alt="" title="查看"/></a>|
  22. <a href="index.php?act=renameFolder&path=<?php echo $path;?>&dirname=<?php echo $p;?>"><img class="small" src="images/rename.png" alt="" title="重命名"/></a>|
  23. <a href="index.php?act=copyFolder&path=<?php echo $path;?>&dirname=<?php echo $p;?>"><img class="small" src="images/copy.png" alt="" title="复制"/></a>|
  24. <a href="index.php?act=cutFolder&path=<?php echo $path;?>&dirname=<?php echo $p;?>"><img class="small" src="images/cut.png" alt="" title="剪切"/></a>|
  25. <a href="#" onclick="delFolder('<?php echo $p;?>','<?php echo $path;?>')"><img class="small" src="images/delete.png" alt="" title="删除"/></a>|
  26. </td>
  27. </tr>
  28. <?php
  29. $i++;
  30. }
  31. }
  32. ?>

在dir.func.php里面加入
$sum是全局变量,避免调用的时候被释放,得不到所有目录里面的结果。
递归调用

  1. function dirSize ($path){
  2. $handle = opendir ($path);
  3. $sum = 0;
  4. global $sum;
  5. while(($item = readdir($handle))!== false){
  6. if ($item != "." && $item != ".."){
  7. if(is_file($path."/".$item)){
  8. $sum += filesize($path."/".$item);
  9. // echo $sum;
  10. }
  11. if (is_dir($path."/".$item)){
  12. $func = __FUNCTION__;
  13. $func($path."/".$item);
  14. }
  15. }
  16. }
  17. closedir($handle);
  18. return $sum;
  19. }
  20. $path = "file";
  21. echo dirSize($path);

每次都要把$sum清空为0,不然就会出现累加的情况。
  1. <td><?php $sum=0; echo transByte(dirSize($p));?></td>

查看文件夹

  1. <a href="index.php?path=<?php echo $p;?>" ><img class="small" src="images/show.png" alt="" title="查看"/></a>|
  1. $path=$_REQUEST['path']?$_REQUEST['path']:$path;
  1. if(!$info){
  2. echo "<script text/javascript>alert('空的');location.href='index.php';</script>";
  3. }


返回上一级

  1. <?php
  2. $back = ($path == "file")? "file" : dirname($path);
  3. ?>
如果当前的$path已经是主目录file了,则保持当前的目录,如果不是则返回上一层。

  1. function goBack($back){
  2. location.href="index.php?path="+$back;
  3. }
  1. <li>
  2. <a href="#" title="返回上级目录" onclick="goBack('
  3. <?php echo $back;?>')">
  4. <span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square">
  5. <span class="icon-arrowLeft">
  6. </span>
  7. </span>
  8. </a>
  9. </li>

复制文件夹


目录函数库里面没有直接复制文件夹的函数,
可以通过复制其中的文件和创建目录来实现目录的复制。

  1. }elseif($act == "copyFolder"){
  2. $str = <<<EOF
  3. <form action = "index.php?act=doCopyFolder" method = "post" >
  4. 将文件夹复制到:<input type="text" name = "dstname" placeholder="将文件夹复制到"/></br>
  5. <input type='hidden' name='filename' value='{$filename}'/>
  6. <input type = 'submit' value='文件夹复制'/>
  7. </form>
  8. EOF;
  9. echo $str;
  10. }
若复制的文件夹不存在,新建
如果存在,则需要看是否存在相同名称的文件夹。
如果没有,则创建。
如果有,则把里面的内容复制到里面。














也可以参见简书主页:https://www.jianshu.com/u/482f183ec380
原文地址:https://www.cnblogs.com/dy2903/p/7230391.html