Skip to content

Instantly share code, notes, and snippets.

@kensnyder

kensnyder/output.php

Created Apr 23, 2014
Embed
What would you like to do?
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