473,387 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 1544
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
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...

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.