Skip to content

Instantly share code, notes, and snippets.

@kensnyder
Created April 23, 2014 15:38
Show Gist options
  • Save kensnyder/11220375 to your computer and use it in GitHub Desktop.
Save kensnyder/11220375 to your computer and use it in GitHub Desktop.
Sending a file
<?php
function sendFile($path, $name, $mime) {
$fp = @fopen($path, 'rb');
if (!$fp) {
// error opening file; it may not exist or be readable
header("HTTP/1.0 404 Not Found");
exit(0);
}
$size = filesize($path);
$lastModified = date('D, d M Y H:i:s', filemtime($path));
// Convert most symbols to underscores
$attachmentName = preg_replace('/[^\w._-]+/', '_', $name);
if ($mime) {
header("Content-Type: $mime");
}
header("Content-Length: $size");
header("Last-Modified: $lastModified GMT");
header("Content-Disposition: attachment; filename=$attachmentName");
header("Content-Transfer-Encoding: binary\n");
fpassthru($fp);
fclose($fp);
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment