| @@ -1,50 +1,71 @@ | |||||
| #!/usr/bin/env php | #!/usr/bin/env php | ||||
| <?php | <?php | ||||
| // ------------------------------------------------------ | // ------------------------------------------------------ | ||||
| // dir2json - v0.1.1b | |||||
| // dir2json | |||||
| // | // | ||||
| // by Ryan, 2015 | // by Ryan, 2015 | ||||
| // http://www.ryadel.com/ | // http://www.ryadel.com/ | ||||
| // Gustavo Arnosti Neves - 2018 | |||||
| // https://github.com/tavinus | |||||
| // ------------------------------------------------------ | // ------------------------------------------------------ | ||||
| // Type the following for help: | |||||
| // > php dir2json -h | |||||
| // Use -h or --help for help, eg: | |||||
| // > ./dir2json.php -h | |||||
| // > php dir2json.php --help | |||||
| // ------------------------------------------------------ | // ------------------------------------------------------ | ||||
| $DIR2JSON = '0.2.0'; | |||||
| function dir2json($dir) | function dir2json($dir) | ||||
| { | { | ||||
| $a = []; | |||||
| $dirList = []; | |||||
| $excludes = [ | |||||
| ".", | |||||
| "..", | |||||
| "Thumb.db", | |||||
| "Thumbs.db", | |||||
| ".DS_Store", | |||||
| ".DS_Store?", | |||||
| ".Spotlight-V100", | |||||
| ".Trashes", | |||||
| "ehthumbs.db", | |||||
| ]; | |||||
| if($handler = opendir($dir)) | if($handler = opendir($dir)) | ||||
| { | { | ||||
| while (($content = readdir($handler)) !== FALSE) | while (($content = readdir($handler)) !== FALSE) | ||||
| { | { | ||||
| if ($content != "." && $content != ".." && $content != "Thumb.db") | |||||
| if (!in_array($content, $excludes)) | |||||
| { | { | ||||
| if(is_file($dir."/".$content)) $a[] = $content; | |||||
| else if(is_dir($dir."/".$content)) $a[$content] = dir2json($dir."/".$content); | |||||
| if(is_file($dir."/".$content)) $dirList[] = $content; | |||||
| else if(is_dir($dir."/".$content)) $dirList[$content] = dir2json($dir."/".$content); | |||||
| } | } | ||||
| } | } | ||||
| closedir($handler); | closedir($handler); | ||||
| } | } | ||||
| return $a; | |||||
| return $dirList; | |||||
| } | } | ||||
| $argv1 = $argv[1]; | |||||
| if (stripos($argv1,"-h") !== false) | |||||
| // Long and short help opts | |||||
| if ($argv[1] === "-h" || $argv[1] === "--help") | |||||
| { | { | ||||
| echo <<<EOT | |||||
| echo <<<EOT | |||||
| ------------------------------------------------------ | ------------------------------------------------------ | ||||
| dir2json - v0.1.1b | |||||
| dir2json - v$DIR2JSON | |||||
| by Ryan, 2015 | |||||
| by Ryan & Tavinus, 2015-2018 | |||||
| http://www.ryadel.com/ | http://www.ryadel.com/ | ||||
| https://github.com/Darkseal/dir2json | |||||
| ------------------------------------------------------ | ------------------------------------------------------ | ||||
| USAGE (from CLI): | USAGE (from CLI): | ||||
| > php dir2json <targetFolder> <outputFile> [JSON_OPTIONS] | |||||
| > ./dir2json.php <targetFolder> <outputFile> [JSON_OPTIONS] | |||||
| EXAMPLE: | EXAMPLE: | ||||
| > php dir2json ./images out.json JSON_PRETTY_PRINT | |||||
| > ./dir2json.php ./ ./cache.json JSON_PRETTY_PRINT | |||||
| HELP: | HELP: | ||||
| > php dir2json -h | |||||
| > ./dir2json.php -h | |||||
| JSON_OPTIONS is a bitmask consisting of: | JSON_OPTIONS is a bitmask consisting of: | ||||
| JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, | JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, | ||||
| @@ -58,21 +79,35 @@ for further info on PHP's json_encode function, read here: | |||||
| http://php.net/manual/en/function.json-encode.php | http://php.net/manual/en/function.json-encode.php | ||||
| ------------------------------------------------------ | ------------------------------------------------------ | ||||
| EOT; | EOT; | ||||
| exit; | |||||
| exit(0); | |||||
| } | } | ||||
| $argv2 = $argv[2]; | |||||
| $argv3 = $argv[3]; | |||||
| if (empty($argv3)) $argv3 = 0; | |||||
| else $argv3 = constant($argv3); | |||||
| // Parse parameters | |||||
| $targetFolder = isset($argv[1]) ? $argv[1] : null; | |||||
| $outputFile = isset($argv[2]) ? $argv[2] : null; | |||||
| $jsonOptions = isset($argv[3]) ? $argv[3] : null; | |||||
| $jsonOptions = empty($jsonOptions) ? 0 : constant($jsonOptions); | |||||
| // If we have a folder to read | |||||
| if (!is_dir($targetFolder)) { | |||||
| echo "Cannot open folder $targetFolder\n"; | |||||
| exit(2); | |||||
| } | |||||
| // If we have and output file name | |||||
| if (empty($outputFile)) { | |||||
| echo "Need a valid output file name (empty)\n"; | |||||
| exit(3); | |||||
| } | |||||
| if (empty($argv2)) { | |||||
| echo "invalid arguments"; | |||||
| exit; | |||||
| $arr = dir2json($targetFolder); | |||||
| $json = json_encode($arr, $jsonOptions); | |||||
| if (!file_put_contents($outputFile, $json)) { | |||||
| echo "Could not save output file: $outputFile\n"; | |||||
| exit(4); | |||||
| } | } | ||||
| $arr = dir2json($argv1); | |||||
| $json = json_encode($arr, $argv3); | |||||
| file_put_contents($argv2, $json); | |||||
| exit(0); | |||||
| ?> | ?> | ||||