彰化一整天的論壇

 找回密碼
 立即註冊
查看: 412|回復: 0

【轉貼】PHP image output and browser caching

[複製鏈接]
發表於 2018-5-21 13:57:35 | 顯示全部樓層 |閱讀模式
PHP image output and browser caching

  1. // Return the requested graphic file to the browser
  2. // or a 304 code to use the cached browser copy
  3. function displayGraphicFile ($graphicFileName, $fileType='jpeg') {
  4.   $fileModTime = filemtime($graphicFileName);
  5.   // Getting headers sent by the client.
  6.   $headers = getRequestHeaders();
  7.   // Checking if the client is validating his cache and if it is current.
  8.   if (isset($headers['If-Modified-Since']) && (strtotime($headers['If-Modified-Since']) == $fileModTime)) {

  9.     // Client's cache IS current, so we just respond '304 Not Modified'.
  10.     header('Last-Modified: '.gmdate('D, d M Y H:i:s', $fileModTime).' GMT', true, 304);
  11.   } else {
  12.     // Image not cached or cache outdated, we respond '200 OK' and output the image.
  13.     header('Last-Modified: '.gmdate('D, d M Y H:i:s', $fileModTime).' GMT', true, 200);
  14.     header('Content-type: image/'.$fileType);
  15.     header('Content-transfer-encoding: binary');
  16.     header('Content-length: '.filesize($graphicFileName));
  17.     readfile($graphicFileName);
  18.   }
  19. }
複製代碼
文章來源: https://ernieleseberg.com/php-image-output-and-browser-caching/
回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

 ㄚ母滴雞湯
 員林香純滴雞精

Archiver|手機版|小黑屋|彰化一整天的論壇(Excel,Office)  

GMT+8, 2019-12-10 08:06 , Processed in 0.107234 second(s), 19 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表