471,093 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,093 software developers and data experts.

Re: .htaccess style rewrite with php

Joe
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!
Aug 18 '08 #1
1 1437
Joe
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();
}
Aug 19 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Nel | last post: by
15 posts views Thread by Taki Jeden | last post: by
2 posts views Thread by Geradeaus | last post: by
unicorn
3 posts views Thread by unicorn | last post: by
9 posts views Thread by invertigo | last post: by
3 posts views Thread by Joe | last post: by
4 posts views Thread by adnanjunk | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.