FileManageModel.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 中闽 < 1464674022@qq.com >
  5. * Date: 2019/12/5
  6. * Time: 17:44
  7. */
  8. namespace app\common\model;
  9. class FileManageModel
  10. {
  11. /**
  12. * 文件管理根目录
  13. * @return string
  14. */
  15. public static function getRootDir()
  16. {
  17. return Templet::getRootDir();
  18. }
  19. /**
  20. * 获取所有文件信息
  21. * @param string $name [过滤文件名]
  22. * @param string $relativePath [过滤子目录]
  23. * @return array
  24. */
  25. public static function getFileList($name = "", $relativePath = "")
  26. {
  27. //定义要查看的目录
  28. $rootpath = self::getRootDir() . DS . $relativePath;
  29. $files = [];
  30. if (!file_exists($rootpath)) {
  31. return $files;
  32. }
  33. //遍历目录
  34. $handle = opendir($rootpath);
  35. while (false !== ($f = readdir($handle))) {
  36. //过滤点
  37. if ($f == "." || $f == "..") {
  38. continue;
  39. }
  40. //拼路径
  41. $filepath = rtrim($rootpath, DS) . DS . $f;
  42. //中文编码
  43. // $filename = iconv("gb2312", "utf-8", $f);
  44. $filename = $f;
  45. //过滤
  46. if (isset($name) and !empty($name)) {
  47. if (!ifContain($filename, $name)) {
  48. continue;
  49. }
  50. }
  51. $info = pathinfo($filepath);
  52. $ext = $info['extension']??'';
  53. $filename = str_replace_once("." . $ext, "", $filename);
  54. $key = filetype($filepath) . '-' . $ext . '-' . $filename;
  55. $files[$key] = [
  56. "filename" => $filename,//文件名
  57. "fileext" => $ext,//扩展名
  58. "filetype" => filetype($filepath),//文件or文件夹
  59. "filesize" => filesize($filepath),
  60. "fileatime" => date('Y-m-d H:i:s', fileatime($filepath)),//最后访问的时间
  61. "filectime" => date('Y-m-d H:i:s', filectime($filepath)),//最后改变的时间
  62. "filemtime" => date('Y-m-d H:i:s', filemtime($filepath)),//最后修改的时间,是文件的内容改变
  63. ];
  64. }
  65. //按文件类型和文件名排序
  66. ksort($files);
  67. //重置索引为数字,否則layui表格全选不支持勾选
  68. $data = [];
  69. foreach ($files as $k => $v) {
  70. $data[] = $v;
  71. }
  72. return $data;
  73. }
  74. }