Skip to content

Instantly share code, notes, and snippets.

@gabrielstuff
Created January 22, 2016 14:25
Show Gist options
  • Save gabrielstuff/d1bca56effd234626cfa to your computer and use it in GitHub Desktop.
Save gabrielstuff/d1bca56effd234626cfa to your computer and use it in GitHub Desktop.
Get file to download directly
<?php
if(!empty($_GET['file'])) {
$filename = filter_input(INPUT_GET, 'file', FILTER_SANITIZE_STRING);
if(strpos(basename($filename), 'jpg') !== false) {
$basename = "/content/images/full/";
}else if(strpos(basename($filename), 'mp4') !== false) {
$basename = "/content/videos/full/";
}else{
$basename = "/file/";
}
$filepath = realpath(dirname(__FILE__)).$basename.$filename; // don't accept other directories
//echo $filepath;
$mime = mime_content_type($filepath);
$fp = @fopen($filepath, "rb");
if ($fp)
{
header("Content-type: {$mime}");
header("Content-Length: " . filesize($filepath));
header("Content-Disposition: attachment; filename=$filename");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
fpassthru($fp);
exit;
} else {
echo "file does not exist";
}
} else {
header("HTTP/1.0 404 Not Found");
echo "string";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment