Skip to content

Instantly share code, notes, and snippets.

@memandip
Created July 10, 2019 08:52
Show Gist options
  • Save memandip/f600ed4c81604222e276d0096206700f to your computer and use it in GitHub Desktop.
Save memandip/f600ed4c81604222e276d0096206700f to your computer and use it in GitHub Desktop.
PHP file download
<?php
$filename = 'filepath.html';
$path = __DIR__ . '/web/uploads/';
$download_file = $path . $filename;
if (file_exists($download_file)) {
$extension = explode('.', $filename);
$extension = $extension[count($extension) - 1];
header('Content-Transfer-Encoding: binary');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($path)) . ' GMT');
header('Accept-Ranges: bytes');
header('Content-Length: ' . filesize($download_file));
header('Content-Encoding: none');
header('Content-Type: application/' . $extension);
header('Content-Disposition: attachment; filename=' . $filename);
readfile($download_file);
exit;
} else {
echo 'File does not exists on given path';
}
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment