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.

119 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)) $dirList[] = $content;
  37. else if(is_dir($dir."/".$content)) $dirList[$content] = dir2json($dir."/".$content);
  38. }
  39. }
  40. closedir($handler);
  41. }
  42. return $dirList;
  43. }
  44. function usage($str='', $ret=2) {
  45. if (!empty($str)) echo $str."\n";
  46. echo "For help, try:\n ./dir2json.php -h\n";
  47. exit($ret);
  48. }
  49. // Long and short help opts
  50. $param = isset($argv[1]) ? $argv[1] : '';
  51. if ($param === "-h" || $param === "--help")
  52. {
  53. echo <<<EOT
  54. ------------------------------------------------------
  55. dir2json - v$DIR2JSON
  56. by Ryan & Tavinus, 2015-2018
  57. http://www.ryadel.com/
  58. https://github.com/Darkseal/dir2json
  59. ------------------------------------------------------
  60. USAGE (from CLI):
  61. > ./dir2json.php <targetFolder> <outputFile> [JSON_OPTIONS]
  62. EXAMPLE:
  63. > ./dir2json.php ./ ./cache.json JSON_PRETTY_PRINT
  64. HELP:
  65. > ./dir2json.php -h
  66. JSON_OPTIONS is a bitmask consisting of:
  67. JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK,
  68. JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_PRESERVE_ZERO_FRACTION,
  69. JSON_UNESCAPED_UNICODE, JSON_PARTIAL_OUTPUT_ON_ERROR
  70. The behaviour of these constants is described on the JSON constants page:
  71. http://php.net/manual/en/json.constants.php
  72. for further info on PHP's json_encode function, read here:
  73. http://php.net/manual/en/function.json-encode.php
  74. ------------------------------------------------------
  75. EOT;
  76. exit(0);
  77. }
  78. // Parse parameters
  79. $targetFolder = isset($argv[1]) ? $argv[1] : null;
  80. $outputFile = isset($argv[2]) ? $argv[2] : null;
  81. $jsonOptions = isset($argv[3]) ? $argv[3] : null;
  82. $jsonOptions = empty($jsonOptions) ? 0 : constant($jsonOptions);
  83. // If we have a folder to read
  84. if (!is_dir($targetFolder)) {
  85. if (empty($targetFolder)) $targetFolder = '(empty)';
  86. usage("Cannot open folder $targetFolder", 2);
  87. }
  88. // If we have an output file name
  89. if (empty($outputFile)) {
  90. usage("Need a valid output file name (empty)", 3);
  91. }
  92. $arr = dir2json($targetFolder);
  93. $json = json_encode($arr, $jsonOptions);
  94. if (!file_put_contents($outputFile, $json)) {
  95. usage("Could not save output file: $outputFile", 4);
  96. }
  97. exit(0);
  98. ?>