Advertisement
Guest User

Untitled

a guest
Sep 1st, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.59 KB | None | 0 0
  1. <?php
  2.  
  3. // NOTE: this script will overwrite index.html files so be careful.
  4. //
  5. // Save as generate.php
  6. // Call as:
  7. // php -f generate.php /some/dir
  8.  
  9. // Only run from the commandline
  10. if (!php_sapi_name() == "cli") {
  11.     exit('Access denied.' . PHP_EOL);  
  12. }
  13.  
  14. // Check if the root directory was passed to us on the commandline
  15. if ($argc < 2) {
  16.     exit('Commandline param missing (root directory path)' . PHP_EOL);  
  17. }
  18. $root_dir = $argv[1];
  19.  
  20. // Check if the root dir exists and is a directory
  21. if (!is_dir($root_dir)) {
  22.     exit('Not a directory: ' . $root_dir . PHP_EOL);  
  23. }
  24.  
  25. // Generate the html files with directory contents
  26. generateDirContents($root_dir);
  27.  
  28. // Loops through a directory and generates an index.html file
  29. // with the directory contents
  30. function generateDirContents($rootDir){
  31.     $filesOrDirs = scandir($rootDir);
  32.  
  33.     // Build an array with files and an array with directories
  34.     $files = array();
  35.     $dirs = array();
  36.  
  37.     foreach($filesOrDirs as $key => $value){
  38.         $path = realpath($rootDir.DIRECTORY_SEPARATOR.$value);
  39.         if(!is_dir($path)) {
  40.             $files[] = $path;
  41.         } else if($value != "." && $value != "..") {
  42.             // recursively call ourselves for sub directories
  43.             generateDirContents($path);
  44.             $dirs[] = $path;
  45.         }
  46.     }
  47.     // generate the html
  48.     $html = generateHtml($rootDir, $files, $dirs);
  49.  
  50.     // write the html into an index.html file
  51.     $htmlFile = $rootDir . DIRECTORY_SEPARATOR . 'index.html';
  52.     file_put_contents($htmlFile, $html);
  53.  
  54.  
  55. }
  56.  
  57.  
  58. // Main function to generate the html
  59. function generateHtml($rootDir, $files, $dirs) {
  60.     $html = getHeader($rootDir);
  61.     foreach($dirs as $dir) {
  62.         $html .= getDirHtml($dir);
  63.     }
  64.     foreach($files as $file) {
  65.         $html .= getFileHtml($file);
  66.     }
  67.     $html .= getFooter($rootDir);
  68.     return $html;
  69. }
  70.  
  71. // Html snippet for each directory
  72. function getDirHtml($dir) {
  73.     $html = <<< EOT
  74. <div class="dir">$dir</div>
  75.  
  76. EOT;
  77.     return $html;
  78. }
  79.  
  80. // Html snippet for each file
  81. function getFileHtml($file) {
  82.     $html = <<< EOT
  83. <div class="file">$file</div>
  84.  
  85. EOT;
  86.     return $html;
  87. }
  88.  
  89. // Html snippet for the header
  90. function getHeader($rootDir) {
  91.     $html = <<< EOT
  92. <!DOCTYPE HTML>
  93. <html>
  94. <head>
  95.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  96.     <title>$rootDir</title>
  97. </head>
  98. <body>
  99. <p>header</p>
  100. EOT;
  101.  
  102.     return $html;
  103. }
  104.  
  105. // Html snippet for the footer
  106. function getFooter($rootDir) {
  107.     $html = <<< EOT
  108. <p>footer</p>
  109. </body>
  110. </html>
  111.  
  112. EOT;
  113.     return $html;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement