473,499 Members | 1,576 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1545
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3675
by: Nel | last post by:
Just looking for some general advice on modifying the URL for php. I am using .htaccess to allow a web site to translate example.html to index.php?content=example (below) ErrorDocument 404...
15
5117
by: Taki Jeden | last post by:
Hello everybody Does anybody know why w3c validator can not get pages that use 404 htaccess redirection? I set up two web sites so that clients request non-existent urls, but htaccess redirects...
2
2821
by: Geradeaus | last post by:
I use htaccess to rewrite the url using the following rules : RewriteRule ^(+)/(+)/(+)?$ index.php?lang=$1&page=$2&id=$3 The only problem I have is when http://www.domain.com/admin is given,...
5
1509
by: tommytx | last post by:
Is there any capability of decisions in htaccess. I know you can use in special situations, but I need something like: If a then b etc like in php. In other words I want to redirect the...
5
4979
by: deko | last post by:
I'm trying to redirect requests for /index.php to /mydirectory/index.php If I use an index file in / with only this line: <?php header("Location:http://www.mysite.com/mydirectory/"); ?> that...
3
1225
unicorn
by: unicorn | last post by:
hi, i want to prevent flashGet (or programs like this) from browsing my website and its content... is that possible? or if it is how... through .htaccess you can use rewrite engine like this: ...
9
4382
by: invertigo | last post by:
Hello, Im using this .htaccess file to rewrite my websites URL. Reason i try to do this is to make it more SEO friendly. Code: -------------------------------------------------------- Options...
3
1642
by: Joe | last post by:
Hello, I currently have a file system where images and assets are stored in a directory like this: http://www.domain.com/assets/media/CLIENT_NAME/uploads/CLIENT_DIR/CLIENT_FILES Which holds...
4
6671
by: adnanjunk | last post by:
Hi, Having a little issue with .htaccess not working on windows server. I have clean url's setup using the htaccess file, working fine on linux server. I have researched for a while and found...
8
4039
hsriat
by: hsriat | last post by:
I had a .htaccess file for Rewrite on my server and was working fine. Now when I used the same code in 1and1 hosting, its not working. May be something is wrong or unexpectedly good happening...
0
7132
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7009
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7223
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5475
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4602
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.