Skip to content

Instantly share code, notes, and snippets.

@mediocregopher
Created October 2, 2013 01:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mediocregopher/6788031 to your computer and use it in GitHub Desktop.
Save mediocregopher/6788031 to your computer and use it in GitHub Desktop.
I was using this as a directory index of sorts. It does all the things a normal directory index does, except that it also allows you to download a directory as a tar.gz. I don't remember how I set it up exactly, I remember there were some nginx rewrites needed
<?php
$dir = $_GET['request_filename'];
if (is_dir($dir)) {
if (!preg_match('/\/$/',$dir)) {
header("Location: ".$_SERVER['REQUEST_URI'].'/');
exit;
}
print "$dir<br/><br/>";
$filenames = ls($dir);
foreach($filenames as $full_name) {
$pi = pathinfo($full_name); $relative_name = $pi['basename'];
if (is_dir($full_name)) $relative_name .= '/';
print "<a href=\"$relative_name\">$relative_name</a>";
if (is_dir($full_name) && $relative_name != './' && $relative_name != '../') {
$tmp = rtrim($relative_name,'/');
print " [ <a href=\"$tmp.tar.gz\">tar.gz</a> ]";
}
print "<br/>\n";
}
}
else if (preg_match('/(.+)\.tar\.gz$/',$dir,$matches)) {
$pi = pathinfo($matches[1]);
header("Content-Type: application/octet-stream");
passthru("cd '{$pi['dirname']}' && tar cvf - '{$pi['basename']}' | gzip -c");
exit;
}
else {
show_404();
}
function show_404() {
header("HTTP/1.0 404 Not Found");
require_once("index.html");
exit;
}
function ls($dir) {
$dh = opendir($dir);
$files = array();
while (($file = readdir($dh)) !== false) {
$files[] = $dir.$file;
}
closedir($dh);
asort($files);
return $files;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment