0
if(preg_match('/^[a-zA-Z0-9]+\.(gif|jpe?g)$/',$row['filename'],$matches) &&
    is_readable($row['filepath'].$row['filename'])){    
  echo header('Content-Type: image/'.$matches[1]);    
  echo header('Content-Length: '.filesize($row['filepath'].$row['filename']));    
  echo file_get_contents($row['filepath'].$row['filename']);
} else {
  echo 'cant serve image: '.$row['filename'];
}

当我尝试放置header('Content-Type': image/jpeg)例如时,它只显示一个空白页面和当前 url。

我试图显示图像来替换我的<img>标签。

filepath 包含图像目录的物理路径。

4

2 回答 2

1

不确定它会解决整个问题,但您不应该echo在函数的返回值上使用header:发送标头,只需使用header函数,不要回显任何内容:

header('Content-Type: image/'.$matches[1]);

无效图像(即 Firefox 显示其 URL 而不是其内容)通常是由图像日期之前/之后的无效输出引起的——请确保除了图像本身之外没有任何内容发送到浏览器。

于 2009-11-10T06:47:19.273 回答
0

你不需要echo header,只是header

于 2009-11-10T06:46:46.777 回答