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.

78 lines
2.0 KiB

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