Php 图像不';t使用readfile/file\u get\u内容显示

Php 图像不';t使用readfile/file\u get\u内容显示,php,header,types,file-get-contents,Php,Header,Types,File Get Contents,例如,当我尝试放置标题('Content-Type':image/jpeg)时,它只显示一个空白页面和当前url 我试图显示一个图像来替换我的标签 filepath包含图像目录的物理路径。您不需要回送头文件,只需头文件不确定它是否能解决整个问题,但不应在函数的返回值上使用回送头文件:要发送头文件,只需使用头文件函数,不要附和任何东西: if(preg_match('/^[a-zA-Z0-9]+\.(gif|jpe?g)$/',$row['filename'],$matches) &&am

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

我试图显示一个图像来替换我的
标签


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

您不需要
回送头文件
,只需
头文件
不确定它是否能解决整个问题,但不应在函数的返回值上使用
回送头文件:要发送头文件,只需使用
头文件
函数,不要附和任何东西:

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'];
}
无效图像(即Firefox显示其URL而不是其内容)通常是由图像日期之前/之后的无效输出引起的——请确保除了图像本身之外,没有任何内容发送到浏览器

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