A PHP CLI script to ouput the contents of a whole directory tree to a JSON object.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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