On Aug 18, 5:04 pm, Joe <j...@faceh.comwrote:
On Aug 5, 8:28 pm, Sjoerd <sjoer...@gmail.comwrote:
On 5 aug, 18:29, Joe <j...@faceh.comwrote:
the issue with that is how to return image
files, or pdf files, or .doc files, etc. as that file type.
You have to provide some headers, like Content-Type, Content-Length,
Content-Disposition. Here's an example:
<?php
class FileServer {
function serve($path) {
$mime = self::getMimeType($path);
$size = self::getFilesize($path);
header('Content-Type: '.$mime);
header('Content-Length: '.$size);
readfile($path);
}
function getMimeType($path) {
$extension = strtolower(substr(strrchr($path, "."),
1));
if ($extension == 'jpg') return 'image/jpeg';
if ($extension == 'jpeg') return 'image/jpeg';
if ($extension == 'png') return 'image/png';
return `file -bi $path`; // uses the 'file' command,
available on unix.
}
function getFilesize($path) {
return filesize($path);
}}
?>
That looks pretty promising, I'll give it a try, thanks!
I can get this working in firefox and safari, but I'm having problems
with PDF's in Internet Explorer. I had it working at one stage, but
the filename of the downloaded file was showing as the URL of the page
accessed, rather than the filename of the file. Here is my code:
$file = base_path()."test.pdf";
if (file_exists($file))
{
// Get mime type
$mime = function-here-to-get-mime-type;
// Force download?
if ($mime == 'application/x-download') {
$disposition = "attachment";
}
else {
$disposition = "inline";
}
// Load asset
$name = basename($file);
header('Cache-Control: no-cache');
header('Content-transfer-encoding: 8bit');
header('Content-Length: '.filesize($file));
header('Content-Type: '.$mime.'; name="'.$name.'"');
header('Content-Disposition: '.$disposition.'; filename="'.
$name.'"');
readfile($file);
exit();
}