Home >Backend Development >PHP Tutorial >PHP download file code (compatible with ie6 browser)

PHP download file code (compatible with ie6 browser)

WBOY
WBOYOriginal
2016-07-25 08:53:031091browse
  1. $filename = "./".$_REQUEST['name']; //Specific path, as long as $filename is the full path of the file you want to download
  2. if (!is_file($ filename)) {//Check whether the file exists.
  3. die('The downloaded file seems to have eloped with someone!');
  4. }
  5. $filepath = str_replace('\', '/', realpath($filename)) ;
  6. $filesize = filesize($filepath);
  7. $filename = substr(strrchr('/'.$filepath, '/'), 1);
  8. $extension = strtolower(substr(strrchr($filepath, '.' ), 1));
  9. // use this unless you want to find the mime type based on extension, file suffix format, not interpreted.
  10. $mime = array('application/octet-stream');
  11. header('Content -Type: '.$mime);
  12. header('Content-Disposition: attachment; filename="'.$filename.'"');
  13. header('Content-Transfer-Encoding: binary');
  14. header(' Content-Length: '.sprintf('%d', $filesize));
  15. header('Expires: 0');
  16. // check for IE only headers, cheating IE detection, you know.
  17. if (isset ($_SERVER['HTTP_USER_AGENT']) &&((strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false)))
  18. {
  19. header('Cache-Control: must-revalidate, post-check =0, pre-check=0');
  20. header('Pragma: public');
  21. } // bbs.it-home.org
  22. else
  23. {
  24. header('Pragma: no-cache');
  25. }
  26. $handle = fopen($filepath, 'rb');
  27. fpassthru($handle);
  28. fclose($handle);
Copy code

Instructions: The above code was found by Brother Feng from a foreign website and can be used for personal testing.

The script editor collected and organized it, thank the original author Feng Ge for producing it~~~



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn