PHP手册中有this example:
<?php
// open the file in a binary mode
$name = './img/ok.png';
$fp = fopen($name, 'rb');
// send the right headers
header("Content-Type: image/png");
header("Content-Length: " . filesize($name));
// dump the picture and stop the script
fpassthru($fp);
exit;
?>
的要点是,你必须发送一个Content-Type头。另外,在<?php ... ?>
标签之前或之后,您必须小心,在文件中不要包含任何额外的空白(如换行符)。
<?php
$name = './img/ok.png';
$fp = fopen($name, 'rb');
header("Content-Type: image/png");
header("Content-Length: " . filesize($name));
fpassthru($fp);
你仍然需要谨慎避免在顶部空白:
正如评论所说,你可以通过省略?>
标签避免多余的空白在脚本结束的危险剧本。一个特别棘手的空白形式是UTF-8 BOM。为避免这种情况,请确保将脚本保存为“ANSI”(记事本)或“ASCII”或“无签名的UTF-8”(Emacs)或类似文件。
只需添加一些安全性,因此像``攻击可避免 – mixdev 2015-11-29 16:01:57