fileVerification.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\utils;
  12. use Exception;
  13. /**
  14. * 签名计算
  15. * Class fileVerification
  16. * @package crmeb\utils
  17. */
  18. class fileVerification
  19. {
  20. public $path = "";
  21. public $fileValue = "";
  22. /**
  23. * 项目路径
  24. * @param string $path
  25. * @return string
  26. * @throws Exception
  27. */
  28. public function getSignature(string $path): string
  29. {
  30. if (!is_dir($path) && !is_file($path)) {
  31. throw new Exception($path . " 不是有效的文件或目录!");
  32. }
  33. $appPath = $path . DS . 'app';
  34. if (!is_dir($appPath)) {
  35. throw new Exception($appPath . " 不是有效的目录!");
  36. }
  37. $crmebPath = $path . DS . 'crmeb';
  38. if (!is_dir($crmebPath)) {
  39. throw new Exception($crmebPath . " 不是有效的目录!");
  40. }
  41. $this->path = $appPath;
  42. $this->getFileSignature($appPath);
  43. $this->path = $crmebPath;
  44. $this->getFileSignature($crmebPath);
  45. return md5($this->fileValue);
  46. }
  47. /**
  48. * 计算签名
  49. * @param string $path
  50. * @return void
  51. * @throws Exception
  52. */
  53. public function getFileSignature(string $path)
  54. {
  55. if (!is_dir($path)) {
  56. $this->fileValue .= @md5_file($path);
  57. } else {
  58. if (!$dh = opendir($path)) throw new Exception($path . " File open failed!");
  59. while (($file = readdir($dh)) != false) {
  60. if ($file == "." || $file == "..") {
  61. continue;
  62. } else {
  63. $this->getFileSignature($path . DS . $file);
  64. }
  65. }
  66. closedir($dh);
  67. }
  68. }
  69. }