Last active
August 29, 2015 14:13
-
-
Save Nickfost/e5cb3c647dac789ad529 to your computer and use it in GitHub Desktop.
This gist allows you to create simple downloads page for a whole directory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Simple downloads page. | |
* @author Nick Foster <[email protected]> | |
*/ | |
// CONFIG | |
$privatedir = "/"; | |
$publicdir = "/"; | |
$title = "My Downloads"; | |
if(isset($_GET['FILE'])){ | |
if (file_exists($privatedir.$_GET['FILE'])) { | |
ignore_user_abort(true); | |
set_time_limit(0); // disable the time limit for this script | |
$path = "http://".$_SERVER['HTTP_HOST']."/".$publicdir; // change the path to fit your websites document structure | |
$dl_file = $_GET['FILE']; | |
$dl_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\].]|[\.]{2,})", '', $dl_file); // simple file name validation | |
$dl_file = filter_var($dl_file, FILTER_SANITIZE_URL); // Remove (more) invalid characters | |
$fullPath = $path.$dl_file; | |
if ($fd = fopen ($fullPath, "r")) { | |
$path_parts = pathinfo($fullPath); | |
header("Content-type: application/octet-stream"); | |
header("Content-Disposition: filename=\"".$path_parts["basename"]."\""); | |
header("Cache-control: private"); | |
while(!feof($fd)) { | |
$buffer = fread($fd, 2048); | |
echo $buffer; | |
} | |
} | |
fclose ($fd); | |
exit; | |
} | |
} | |
// This minifys the whole page | |
function html($html){ | |
echo trim($html); | |
} | |
html("<!DOCTYPE html>"); | |
html("<html lang=\"en\">"); | |
html(" <head>"); | |
html(" <meta charset=\"utf-8\">"); | |
html(" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\">"); | |
html(" <meta name=\"robots\" content=\"index, follow\">"); | |
html(" <meta name=\"revisit-after\" content=\"1 day\">"); | |
html(" <meta name=\"description\" content=\"".$title."\">"); | |
html(" <title>".$title."</title>"); | |
html(" <link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css\">"); | |
html(" <link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap-theme.min.css\">"); | |
html(" </head>"); | |
html(" <body class=\"container\">"); | |
html(" <p class=\"text-center h1\">".$title."</p>"); | |
html(" <div class=\"text-center list-group\">"); | |
$files = scandir($privatedir); | |
$files = array_reverse($files); | |
foreach($files as $file){ | |
if($file != "." & $file != ".."){ | |
html(" <a class=\"text-center list-group-item\" href=\"?FILE=".$file."\">".$file."</a></p>"); | |
} | |
} | |
html(" </div>"); | |
html(" </body>"); | |
html("</html>"); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment