[Php] & lt ;? PhpfunctiondownloadFile ($ fullPath) {// Mustbefreshstartif (headers_sent () // checkifanyheaderhasbeensentdie (& amp; #39; HeadersSent & amp ;#
[Php]
Function downloadFile ($ fullPath ){
// Must be fresh start
If (headers_sent () // check if any header has been sent
Die ('headers sent'); // Equivalent to exit ()
// Required for some browsers
If (ini_get ('zlib. output_compression ') // Gets the value of a configuration option
Ini_set ('zlib. output_compression ', 'off'); // This plugin allows PHP transparent reading and writing to gzip(.gz) compressed files.
// File Exists?
If (file_exists ($ fullPath )){
// Parse Info/Get Extension
$ Fsize = filesize ($ fullPath );
$ Path_parts = pathinfo ($ fullPath); // return the file path information.
/*
$ Path_parts = pathinfo ("/www/htdocs/index.html ");
Echo $ path_parts ["dirname"]. "\ n ";
Echo $ path_parts ["basename"]. "\ n ";
Echo $ path_parts ["extension"]. "\ n"; // suffix
The returned information is:
/Www/htdocs
Index.html
Html
*/
$ Ext = strtolower ($ path_parts ["extension"]); // converts a string to lowercase.
// Determine Content Type
Switch ($ ext ){
Case "pdf": $ ctype = "application/pdf"; break;
Case "exe": $ ctype = "application/octet-stream"; break;
Case "zip": $ ctype = "application/zip"; break;
Case "doc": $ ctype = "application/msword"; break;
Case "xls": $ ctype = "application/vnd. ms-excel"; break;
Case "ppt": $ ctype = "application/vnd. ms-powerpoint"; break;
Case "gif": $ ctype = "image/gif"; break;
Case "png": $ ctype = "image/png"; break;
Case "jpeg ":
Case "jpg": $ ctype = "image/jpg"; break;
Default: $ ctype = "application/force-download ";
}
Header ("Pragma: public"); // required indicates that the response can be saved in any cache.
/*
The same that "Cache-Control: public"
Public: indicates that the response can be saved in any cache, even if the response is usually not cached or can only be cached in non-shared cache.
Private: indicates that part or all parts of the response message are prepared for a user and cannot be stored in the shared cache. Enable the source server
The server can declare a specific part of the response to target a user and the request to other users is invalid. A private
(Non-shared) the cache can cache this response.
No-cache: if no-cache control command does not specify a field-name, a cache cannot use this response
If the source server successfully reverifies it, the subsequent requests are met. This allows the source server to prevent response
It should be cached and saved, even if the cache has been set, the old response can be returned to the client.
If the no-cache control command specifies one or more field-names, the cache can use this response to meet
Subsequent requests, but this is subject to other restrictions on the cache. However, the specified filed-name must not be requested later
If the response is not successfully reverified on the source server. This allows the source server to prevent
The cache removes the reuse of some header fields in the response, but allows the cache to save the remaining part of the response.
/*
The Pragma header specifies directives for proxy and gateway systems.
Since Multicast proxy systems may exist between a client and server, Pragma
Headers must pass through each proxy. When the Pragma header reaches
The server, the header may be ignored by the server software.
The only directive defined in HTTP/1.0 is the no-cache directive. It is
Used to tell caching proxies to contact the server for the requested
Document, instead of using its local cache. This allows the client
Request the most up-to-date document from the original web server,
Without authentication ing a cached copy from an intermediate proxy server.
The Pragma header is an HTTP 1.0 feature, and is maintained in HTTP 1.1
For backward compatibility. No new Pragma ctictives will be defined in
The future.
*/
Header ("Expires: 0 ");
/*
The Expires object header field (entity-header) indicates when the response will be treated as obsolete. An old cache item
Cannot be cached (one proxy cache or one user proxy cache), unless this cache item is cached by the source server
(Or it is cached by an intermediate cache with a fresh copy of the object.
0 indicates that the request expires immediately, indicating that the request is not cached.
*/
Header ("Cache-Control: must-revalidate, post-check = 0, pre-check = 0 ");
/*
Post-check and pre-check cache control directives must appear together
In pairs other wise they are ignored.
*/
Header ("Cache-Control: private", false); // required for certain browsers
Header ("Content-Type: $ ctype ");
Header ("Content-Disposition: attachment; filename = \" ". basename ($ fullPath )."\";");
Header ("Content-Transfer-Encoding: binary ");
Header ("Content-Length:". $ fsize );
Ob_clean (); // Clean (erase) the output buffer
Flush (); // refresh the buffer of the PHP program, regardless of the situation in which PHP is executed (CGI, web server, etc ). This function sends all the output of the program so far to the user's browser.
Readfile ($ fullPath); // read a file and write it to the output buffer.
} Else
Die ('file Not Found ');
}
?>
This is the sample code in the PHP Manual. if you do not understand it, you can translate it.
Below is
©Kekehu/technical resources/2009.02.17/10: 15/ipv6pv
The Cache of web pages is controlled by "cache-control" in the HTTP message header. common values include private, no-Cache, max-age, and must-revalidate, the default value is private. The function of browser review is divided into the following situations:
(1) open a new window
If the values are private, no-cache, and must-revalidate, the server will be accessed again when a new window is opened.
If the max-age value is specified, the server will not be accessed again within the time range. for example:
Cache-control: max-age = 5 (indicating that the server will not be accessed again within 5 seconds after the webpage is accessed)
(2) press enter in the address bar.
If the value is private or must-revalidate, the server will be accessed only during the first access and will not be accessed in the future.
The value is no-cache.
If the value is max-age, the access will not be repeated before expiration.
(3) back and press the button
If the values are private, must-revalidate, and max-age, no re-access is performed,
If the value is no-cache, access is repeated each time.
(4) press the refresh button
No matter what the value is, it will be accessed again
When the Cache-control value is "no-cache", accessing this page does not leave page backup in the temporary article folder on the Internet.
In addition, specifying the "Expires" value also affects the cache. For example, if you specify the Expires value as a time that has passed, if you repeatedly press enter in the address bar when accessing this network, the access will be repeated at: Expires: Fri, 31 Dec 1999 16:00:00 GMT
For example, disable page caching in IE.
Http response message header settings:
CacheControl = no-cache
Pragma = no-cache
Expires =-1
Expires is a good stuff. if the webpage on the server changes frequently, set it to-1, indicating that it will expire immediately. If a webpage is updated at every day, you can set Expires to the next day.
When CacheControl = no-cache is specified on the HTTP1.1 server, the browser does not cache the webpage.
The Cache-Control title cannot be used for legacy HTTP 1.0 servers.
To be backward compatible with HTTP 1.0 servers, IE uses the Pragma: no-cache title to provide special support for HTTP.
If the client communicates with the server through a secure connection (https: //) and the server returns the Pragma: no-cache title in the response,
Internet Explorer does not cache this response. Note: Pragma: no-cache prevents caching only when used in secure connections. if used on a non-secure page, the processing method is the same as Expires:-1, this page will be cached but marked as expired immediately.
Author: wolinxuebin