A PHP CLI script to ouput the contents of a whole directory tree to a JSON object.
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.0 KiB

  1. #!/usr/bin/env php
  2. <?php
  3. // ------------------------------------------------------
  4. // dir2json
  5. //
  6. // by Ryan, 2015
  7. // http://www.ryadel.com/
  8. // Gustavo Arnosti Neves - 2018
  9. // https://github.com/tavinus
  10. // ------------------------------------------------------
  11. // Use -h or --help for help, eg:
  12. // > ./dir2json.php -h
  13. // > php dir2json.php --help
  14. // ------------------------------------------------------
  15. $DIR2JSON = '0.2.1';
  16. function dir2json($dir)
  17. {
  18. $dirList = [];
  19. $excludes = [
  20. ".",
  21. "..",
  22. "Thumb.db",
  23. "Thumbs.db",
  24. ".DS_Store",
  25. ".DS_Store?",
  26. ".Spotlight-V100",
  27. ".Trashes",
  28. "ehthumbs.db",
  29. ];
  30. if($handler = opendir($dir))
  31. {
  32. while (($content = readdir($handler)) !== FALSE)
  33. {
  34. if (!in_array($content, $excludes))
  35. {
  36. if(is_file($dir."/".$content))
  37. $dirList[] = $content;
  38. else if(is_dir($dir."/".$content))
  39. $dirList[$content] = dir2json($dir."/".$content);
  40. }
  41. }
  42. closedir($handler);
  43. }
  44. return $dirList;
  45. }
  46. function usage($str='', $ret=2) {
  47. if (!empty($str)) echo $str."\n";
  48. echo "For help, try:\n ./dir2json.php -h\n";
  49. exit($ret);
  50. }
  51. // Long and short help opts
  52. $param = isset($argv[1]) ? $argv[1] : '';
  53. if ($param === "-h" || $param === "--help")
  54. {
  55. echo <<<EOT
  56. ------------------------------------------------------
  57. dir2json - v$DIR2JSON
  58. by Ryan & Tavinus, 2015-2018
  59. http://www.ryadel.com/
  60. https://github.com/Darkseal/dir2json
  61. ------------------------------------------------------
  62. USAGE (from CLI):
  63. > ./dir2json.php <targetFolder> <outputFile> [JSON_OPTIONS]
  64. EXAMPLE:
  65. > ./dir2json.php ./ ./cache.json JSON_PRETTY_PRINT
  66. HELP:
  67. > ./dir2json.php -h
  68. JSON_OPTIONS is a bitmask consisting of:
  69. JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK,
  70. JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_PRESERVE_ZERO_FRACTION,
  71. JSON_UNESCAPED_UNICODE, JSON_PARTIAL_OUTPUT_ON_ERROR
  72. The behaviour of these constants is described on the JSON constants page:
  73. http://php.net/manual/en/json.constants.php
  74. for further info on PHP's json_encode function, read here:
  75. http://php.net/manual/en/function.json-encode.php
  76. ------------------------------------------------------
  77. EOT;
  78. exit(0);
  79. }
  80. // Parse parameters
  81. $targetFolder = isset($argv[1]) ? $argv[1] : null;
  82. $outputFile = isset($argv[2]) ? $argv[2] : null;
  83. $jsonOptions = isset($argv[3]) ? $argv[3] : null;
  84. $jsonOptions = empty($jsonOptions) ? 0 : constant($jsonOptions);
  85. // If we have a folder to read
  86. if (!is_dir($targetFolder)) {
  87. if (empty($targetFolder)) $targetFolder = '(empty)';
  88. usage("Cannot open folder $targetFolder", 2);
  89. }
  90. // If we have an output file name
  91. if (empty($outputFile)) {
  92. usage("Need a valid output file name (empty)", 3);
  93. }
  94. $arr = dir2json($targetFolder);
  95. $json = json_encode($arr, $jsonOptions);
  96. if (!file_put_contents($outputFile, $json)) {
  97. usage("Could not save output file: $outputFile", 4);
  98. }
  99. exit(0);
  100. ?>