|
Part:
1
2
3
4
5
6
7
(Continued from previous part...)
Generating Non-HTML Entity Body
Sometimes, you may want to send back information in the entity body that are
not in the HTML format, for example, a PDF document, or a MS Word Document. In this
case, we have to set Content-Type, Content-Length and other header lines carefully
to provide correct information about the entity body for the client program.
Here is a sample PHP script to show you how to set header lines for different types
of data in the entity body.
<?php #GetFile.php
# Copyright (c) 2005 by Dr. Herong Yang, http://www.herongyang.com/
#
$ok = array_key_exists('QUERY_STRING', $_SERVER);
if ($ok) {
$p = $_SERVER['QUERY_STRING'];
$ok = strlen($p)>0 && file_exists($p);
}
if ($ok) {
if (strpos($p,".html")!=false) {
header("Content-Type: text/html");
} else if (strpos($p,".gif")!=false) {
header("Content-Type: image/gif");
} else if (strpos($p,".pdf")!=false) {
header("Content-Type: application/pdf");
} else if (strpos($p,".doc")!=false) {
header("Content-Type: application/msword");
} else {
$ok = false;
}
}
if ($ok) {
header("Content-Length: ".filesize($p));
readfile($p);
} else {
print("<html><body>Bad request.</body></html>");
}
?>
Ideas used in this script:
- The objective is to send back the content of the requested file
in entity body, and set the Content-Type and Content-Length header lines correctly.
- The requested file name is given in the query string of the HTTP request.
array_key_exists() is used to make sure the query string is provided to avoid array index error.
- The extension of the requested file name is checked to determine the Content-Type
header line. strpos() is used as a Boolean value to check the file name extension.
- Then the requested file size is checked to set the Content-Length header line.
- Then the requested file is outputted to the HTTP response with a single function readfile().
This function name is a little bit misleading, but the function is very useful.
- If any thing goes wrong, a HTML entity is returned with an error message. A Boolean variable $ok
is used to monitor the error condition.
Now let's see how this page works. Assuming I have GetFile.php, hello.html, dot.gif, hello.pdf,
and hello.doc copied to c:\inetpub\wwwroot directory. Of course, my IIS is configured to the PHP engine.
1. Use IE (Internet Explorer) to request: http://localhost/GetFile.php?hello.html,
you should see the hello message properly displayed as a HTML document.
2. Use IE to request: http://localhost/GetFile.php?dot.gif,
you should see a tiny dot displayed as an image.
3. Use IE to request: http://localhost/GetFile.php?hello.pdf,
you should see IE calling Adobe Reader to display the hello message as a PDF document.
4. Use IE to request: http://localhost/GetFile.php?hello.doc,
you should see IE calling MS Word to display the hello message as a MS Word document.
5. Use IE to request: http://localhost/GetFile.php?any.file,
you should see IE displaying an error message. The reason, of course, is that
the requested file doesn't exist.
6. Use IE to request: http://localhost/GetFile.php
you should see IE displaying an error message. The reason is that there is no query string
in the HTTP request.
(Continued on next part...)
Part:
1
2
3
4
5
6
7
|