Skip to content

Instantly share code, notes, and snippets.

@h4cc
Created August 22, 2014 08:39
Show Gist options
  • Save h4cc/38246112ad46310bc683 to your computer and use it in GitHub Desktop.
Save h4cc/38246112ad46310bc683 to your computer and use it in GitHub Desktop.
<?php
/**
* Will handle downloads for $_REQUEST['file']
* @author Julius Beckmann
*/
function handleForcedDownloads($parameterName='file', $exit=true)
{
if(isset($_REQUEST[$parameterName])) {
$file = $_REQUEST[$parameterName];
if(false !== stripos($file, '../')) {
die("ERROR: Fehlerhafter Download Link.");
}
// Working on purpose with cwd.
if(!file_exists($file)) {
die("ERROR: Datei konnte nicht gefunden werden.");
}
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Content-disposition: attachment; filename="'.basename($file).'"');
readfile($file);
if($exit) {
die();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment