A PHP CLI script to ouput the contents of a whole directory tree to a JSON object.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 regels
4.4 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.3.0';
  16. function dir2json($dir, $sortOrder = SORT_ASC, $sortFlag = SORT_LOCALE_STRING)
  17. {
  18. $dirList = [];
  19. $tmpList = []; // used to sort files and dirs together
  20. $excludes = [
  21. ".",
  22. "..",
  23. "Thumb.db",
  24. "Thumbs.db",
  25. ".DS_Store",
  26. ".DS_Store?",
  27. ".Spotlight-V100",
  28. ".Trashes",
  29. "ehthumbs.db",
  30. ];
  31. if($handler = opendir($dir))
  32. {
  33. while (($content = readdir($handler)) !== FALSE)
  34. {
  35. if (!in_array($content, $excludes))
  36. {
  37. $tmpList[] = strtolower($content);
  38. //$tmpList[] = $content;
  39. if(is_file($dir."/".$content))
  40. $dirList[] = $content;
  41. else if(is_dir($dir."/".$content))
  42. $dirList[$content] = dir2json($dir."/".$content, $sortOrder, $sortFlag);
  43. }
  44. }
  45. closedir($handler);
  46. }
  47. // sort based on file/folder name
  48. array_multisort($tmpList, $sortOrder, $sortFlag, $dirList);
  49. return $dirList;
  50. }
  51. function usage($str='', $ret=2) {
  52. if (!empty($str)) echo $str."\n";
  53. echo "For help, try:\n ./dir2json.php -h\n";
  54. exit($ret);
  55. }
  56. // Long and short help opts
  57. $param = isset($argv[1]) ? $argv[1] : '';
  58. if ($param === "-h" || $param === "--help")
  59. {
  60. echo <<<EOT
  61. ------------------------------------------------------
  62. dir2json - v$DIR2JSON
  63. by Ryan & Tavinus, 2015-2018
  64. http://www.ryadel.com/
  65. https://github.com/Darkseal/dir2json
  66. ------------------------------------------------------
  67. USAGE (from CLI):
  68. > ./dir2json.php <targetFolder> <outputFile> [JSON_OPTIONS] [SORT_ORDER] [SORT_FLAG]
  69. EXAMPLE:
  70. > ./dir2json.php ./ ./cache.json JSON_PRETTY_PRINT SORT_DESC SORT_NATURAL
  71. HELP:
  72. > ./dir2json.php -h
  73. ------------------------------------------------------
  74. JSON_OPTIONS is a bitmask consisting of: (default: 0)
  75. JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK,
  76. JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_PRESERVE_ZERO_FRACTION,
  77. JSON_UNESCAPED_UNICODE, JSON_PARTIAL_OUTPUT_ON_ERROR
  78. The behaviour of these constants is described on the JSON constants page:
  79. http://php.net/manual/en/json.constants.php
  80. for further info on PHP's json_encode function, read here:
  81. http://php.net/manual/en/function.json-encode.php
  82. ------------------------------------------------------
  83. Sorting is done by the PHP funcion array_multisort()
  84. http://php.net/manual/en/function.array-multisort.php
  85. SORT_ORDER can be either:
  86. SORT_ASC - ascendingly (default)
  87. SORT_DESC - descendingly
  88. SORT_FLAGS defaults to SORT_LOCALE_STRING, always case insensitive:
  89. SORT_REGULAR - compare items normally (don't change types)
  90. SORT_NUMERIC - compare items numerically
  91. SORT_STRING - compare items as strings
  92. SORT_LOCALE_STRING - compare items as strings, based on the current locale.
  93. SORT_NATURAL - compare items as strings using "natural ordering" like natsort()
  94. ------------------------------------------------------
  95. EOT;
  96. exit(0);
  97. }
  98. // Parse parameters
  99. $targetFolder = isset($argv[1]) ? $argv[1] : null;
  100. $outputFile = isset($argv[2]) ? $argv[2] : null;
  101. $jsonOptions = isset($argv[3]) ? $argv[3] : null;
  102. $jsonOptions = empty($jsonOptions) ? 0 : constant($jsonOptions);
  103. $sortOrder = isset($argv[4]) ? $argv[4] : null;
  104. $sortOrder = empty($sortOrder) ? SORT_ASC : constant($sortOrder);
  105. $sortFlag = isset($argv[5]) ? $argv[5] : null;
  106. $sortFlag = empty($sortFlag) ? SORT_LOCALE_STRING : constant($sortFlag);
  107. // If we have a folder to read
  108. if (!is_dir($targetFolder)) {
  109. if (empty($targetFolder)) $targetFolder = '(empty)';
  110. usage("Cannot open folder $targetFolder", 2);
  111. }
  112. // If we have an output file name
  113. if (empty($outputFile)) {
  114. usage("Need a valid output file name (empty)", 3);
  115. }
  116. $arr = dir2json($targetFolder, $sortOrder, $sortFlag);
  117. //var_dump($arr);
  118. $json = json_encode($arr, $jsonOptions);
  119. if (!file_put_contents($outputFile, $json)) {
  120. usage("Could not save output file: $outputFile", 4);
  121. }
  122. exit(0);
  123. ?>