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.

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