使用URL参数和XSendFile在浏览器中缓存图像

koj*_*ow7 9 php apache x-sendfile browser-cache reactjs

我试图建立一个站点,以便用户只能访问自己的图像和音频文件。为此,我在URL中使用了变量,例如:

 <img src="/public/get_file.php?type=image&file=pic001.png" />
Run Code Online (Sandbox Code Playgroud)

在前端,我正在使用React JS(不确定这对这个问题是否重要)。但是在后端,PHP脚本将验证用户已登录(仅通过检查一个简单的SESSION变量),并在用户的数据目录中查找该文件(基于其SESSION变量中的用户ID)。如果存在,它将使用XSendFile将其返回给用户。

我遇到的问题是,每次用户尝试访问文件时,在加载文件之前都会有一些延迟。这告诉我它们可能没有被浏览器缓存。

为什么文件没有被缓存?它与URL参数有关还是与PHP / XSendFile的使用有关?

如何允许我的文件(图像/音频)被缓存?

更新资料

根据要求,这里是我的get_file.php:

<?php

        session_start();

        if (empty($_SESSION['auth']['id'])){
                exit;
        }

        require_once("../../api_modules/settings.php");

        $developer_id = $_SESSION['auth']['id'];

        $type = preg_replace('/[^-a-zA-Z0-9_]/', '', $_GET['type']);
        $file = preg_replace('/[^-a-zA-Z0-9_\.]/', '', $_GET['file']);

        $file = preg_replace('/[\.]{2,}/', '', $file);

        $project_id = preg_replace('/[^0-9]/', '', $_GET['project_id']);
        $developer_id = preg_replace('/[^0-9]/', '', $developer_id);

        if ($type == "image")
                $file = FILEPATH ."files/$developer_id/$project_id/images/thumbs/88x88/$file";
        else if ($type == "full_image")
                $file = FILEPATH ."files/$developer_id/$project_id/images/originals/$file";
        else if ($type == "audio"){
                $file = FILEPATH ."files/$developer_id/$project_id/audio/$file";
        }
        else {
                exit;
        }


        header("X-Sendfile: $file");
        header("Content-type: application/octet-stream");
        header('Content-Disposition: attachment; filename="' . basename($file) . '"');
Run Code Online (Sandbox Code Playgroud)

Ale*_*lex 0

我认为这是重复的

但由于有赏金,让我们尝试将您的代码与建议的解决方案一起调整。

您需要检查 Apache 是否已启用 mod_expires 和 mod_headers 并正常工作。

然后在开始实际输出之前检查文件是否被修改:

...
$last_modified_time = filemtime($file); 
$etag = md5_file($file);
// always send headers
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT"); 
header("Etag: $etag"); 
// exit if not modified
if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time || 
    @trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) { 
    header("HTTP/1.1 304 Not Modified"); 
    exit; 
} else {
    header("X-Sendfile: $file");
    header("Content-type: application/octet-stream");
    header('Content-Disposition: attachment; filename="' . basename($file) . '"');
}
Run Code Online (Sandbox Code Playgroud)