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.

79 lines
2.0 KiB

  1. #!/usr/bin/env php
  2. <?php
  3. // ------------------------------------------------------
  4. // dir2json - v0.1.1b
  5. //
  6. // by Ryan, 2015
  7. // http://www.ryadel.com/
  8. // ------------------------------------------------------
  9. // Type the following for help:
  10. // > php dir2json -h
  11. // ------------------------------------------------------
  12. function dir2json($dir)
  13. {
  14. $a = [];
  15. if($handler = opendir($dir))
  16. {
  17. while (($content = readdir($handler)) !== FALSE)
  18. {
  19. if ($content != "." && $content != ".." && $content != "Thumb.db")
  20. {
  21. if(is_file($dir."/".$content)) $a[] = $content;
  22. else if(is_dir($dir."/".$content)) $a[$content] = dir2json($dir."/".$content);
  23. }
  24. }
  25. closedir($handler);
  26. }
  27. return $a;
  28. }
  29. $argv1 = $argv[1];
  30. if (stripos($argv1,"-h") !== false)
  31. {
  32. echo <<<EOT
  33. ------------------------------------------------------
  34. dir2json - v0.1.1b
  35. by Ryan, 2015
  36. http://www.ryadel.com/
  37. ------------------------------------------------------
  38. USAGE (from CLI):
  39. > php dir2json <targetFolder> <outputFile> [JSON_OPTIONS]
  40. EXAMPLE:
  41. > php dir2json ./images out.json JSON_PRETTY_PRINT
  42. HELP:
  43. > php dir2json -h
  44. JSON_OPTIONS is a bitmask consisting of:
  45. JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK,
  46. JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_PRESERVE_ZERO_FRACTION,
  47. JSON_UNESCAPED_UNICODE, JSON_PARTIAL_OUTPUT_ON_ERROR
  48. The behaviour of these constants is described on the JSON constants page:
  49. http://php.net/manual/en/json.constants.php
  50. for further info on PHP's json_encode function, read here:
  51. http://php.net/manual/en/function.json-encode.php
  52. ------------------------------------------------------
  53. EOT;
  54. exit;
  55. }
  56. $argv2 = $argv[2];
  57. $argv3 = $argv[3];
  58. if (empty($argv3)) $argv3 = 0;
  59. else $argv3 = constant($argv3);
  60. if (empty($argv2)) {
  61. echo "invalid arguments";
  62. exit;
  63. }
  64. $arr = dir2json($argv1);
  65. $json = json_encode($arr, $argv3);
  66. file_put_contents($argv2, $json);
  67. ?>