我正在尝试设置一个网站,以便用户只能访问自己的图像和音频文件。为此我在URL中使用变量,例如:
<img src="/public/get_file.php?type=image&file=pic001.png" />
在前端我使用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) . '"');
我认为这是一个duplicate。
但是,既然有赏金,我们可以尝试使用建议的解决方案来调整您的代码。
您需要检查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) . '"');
}
尝试使用这种标头(PHP代码)与header('Cache-Control: must-revalidate');
下载私有文件
$mime_types = array(
'pdf' => 'application/pdf',
'txt' => 'text/plain',
'html' => 'text/html',
'exe' => 'application/octet-stream',
'zip' => 'application/zip',
'doc' => 'application/msword',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'gif' => 'image/gif',
'png' => 'image/png',
'jpeg' => 'image/jpg',
'jpg' => 'image/jpg',
'php' => 'text/plain'
);
if ($mimeType == '') {
$file_ext = strtolower(substr(strrchr($file_path.$file_name_gen, '.'), 1));
if(array_key_exists($file_ext, $mime_types)) $mime_type = $mime_types[$file_ext];
else $mime_type = 'application/force-download';
}
ob_start();
header('Content-Disposition: attachment; filename="' . $file_name_gen . '"');
header('Content-Type: '.$mime_type);
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file_path.$file_name));
ob_clean();
flush();
readfile($file_path.$file_name);
并使用Cache控件为您的解决方案提供最佳选择
Cache-Control: max-age=<seconds>
Cache-Control: max-stale[=<seconds>]
Cache-Control: min-fresh=<seconds>
Cache-Control: no-cache
Cache-Control: no-store
Cache-Control: no-transform
Cache-Control: only-if-cached
BTW。 $file_name_gen
是服务器上的真正文件名,$file_name
是用户看到的更多文件隐私的文件名。
为了记录,这主要是因为session.cache_limiter
的默认值是nocache
。
请参阅http://php.net/manual/en/function.session-cache-limiter.php,了解如何设置符合您需求的不同值。
你如何生成图像?您是否有公共本地目录或由CDN(远程)加载?