【发布时间】:2009-06-03 22:37:39
【问题描述】:
我将所有图片都存储在 webroot 后面(/var/www/ 之前),这意味着 web 服务器无法为我的图片发回缓存标头。我需要添加什么才能使用户的网络缓存正常工作?目前,每次都被同一个浏览器点击。
我页面上的<img> 路径如下所示:
<img src="pic.php?u=1134&i=13513&s=0">
编辑:会不会是因为“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;
?>
【问题讨论】: