我正在编写一个PHP程序,它将从文件系统中获取图像并将其显示在返回的页面上.问题是该文件未存储在/ var/www目录中.它存储在/ var/site/images中.我怎样才能做到这一点?我是否必须用fopen将其读入内存,然后回显内容?
用于fpassthru将文件系统中的内容转储到输出流.事实上,文档fpassthru包含了一个关于你正在尝试做什么的演示:http://us3.php.net/fpassthru
<?php
// open the file in a binary mode
$name = './img/ok.png';
$fp = fopen($name, 'rb');
// send the right headers
// - adjust Content-Type as needed (read last 4 chars of file name)
// -- image/jpeg - jpg
// -- image/png - png
// -- etc.
header("Content-Type: image/png");
header("Content-Length: " . filesize($name));
// dump the picture and stop the script
fpassthru($fp);
fclose($fp);
exit;
?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2091 次 |
| 最近记录: |