为什么从我的Web服务器提供的图像不会缓存在客户端上?

Mic*_*ICE 2 php apache caching

我将所有图像存储在webroot后面(之前/var/www/),这意味着Web服务器无法为我的图片发回缓存头.我需要添加什么才能使用户的Web缓存工作?目前,每次都被同一个浏览器击中.

<img>在我的页面上的路径看起来像这样:

<img src="pic.php?u=1134&i=13513&s=0">
Run Code Online (Sandbox Code Playgroud)

编辑:可能是因为" pic.php?u=1134&i=13513&s=0"不是有效的文件名或什么?

// pic.php
<?php

    // open the file in a binary mode
    $user = $_GET['u'];
    $id = $_GET['i'];
    $s = $_GET['s'];

    if (!isset($user) && !isset($s) && $isset($id))
    {
        // display a lock!
        exit(0);
    }

    require_once("bootstrap_minimal.php"); //setup db connection, etc

    // does this image_id belong to the user?
    $stmt = $db->query('SELECT image_id, user_id, file_name, private FROM images WHERE image_id = ?', $id);
    $obj = $stmt->fetchObject();

    if (is_object($obj))
    {
        // is the picture is the users?
        if ($obj->user_id != $_SESSION['user_id'])
        {
            // is this a private picture?
            if ($obj->private == 1)
            {
                // check permissions...
                // display a lock in needed!
            }
        }
    }
    else
    {
        // display a error pic?!
        exit(0);
    }

    if ($s == 0)
    {
        $picture = $common->getImagePathThumb($obj->file_name);
    }
    else
    {
        $picture = $common->getImagePath($obj->file_name);
    }

    // send the right headers
    header("Content-Type: image/png");
    header("Content-Length: " . filesize($picture));

    $fp = fopen($picture, 'rb');

    // dump the picture and stop the script
    fpassthru($fp);
    exit;
?>
Run Code Online (Sandbox Code Playgroud)

Gre*_*reg 5

您需要添加以下内容:

$expiry = 3600*24*7; // A week
header('Expires: ' . gmdate('D, d M Y H:i:s' time() + $expiry) . ' GMT');
header('Cache-control: private, max-age=' . $expiry);
Run Code Online (Sandbox Code Playgroud)