Skip to content

Instantly share code, notes, and snippets.

@CDRO
Created October 4, 2018 11:07
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 CDRO/6bb1589008dc7cc9a01a8e3dca17f5fe to your computer and use it in GitHub Desktop.
Save CDRO/6bb1589008dc7cc9a01a8e3dca17f5fe to your computer and use it in GitHub Desktop.
A safe single download for files in PHP
<?php
if(file_exists(__FILE__ . '.downloaded')) {
header('Location:/');
die;
}
touch(__FILE__ . '.downloaded');
$file = '__REPLACE_ME_FILE_PATH__';
if(file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="__REPLACE_ME_FILE_NAME__"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
die;
}
?><html><head><title>File does not exist anymore</title><meta http-equiv="refresh" content="3; URL=/"></head><body><h1>The file you tried to access does no longer exist.</h1><h2>You will be automatically redirected</h2></body></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment