使用php下载时通过readfile()函数呈现的图像缺少扩展名
从使用使用php下载时通过readfile()函数呈现的图像缺少扩展名,php,file-io,content-type,Php,File Io,Content Type,从使用readfile函数渲染的图像下载的图像缺少扩展名 getphoto函数代码如下所示: <?php header('Content-Description: File Transfer'); header('Access-Control-Allow-Origin:*'); header('Cache-Control:public, max-age=864000'); header('Content-Type:image/jpg');
readfile
函数渲染的图像下载的图像缺少扩展名
getphoto函数代码如下所示:
<?php
header('Content-Description: File Transfer');
header('Access-Control-Allow-Origin:*');
header('Cache-Control:public, max-age=864000');
header('Content-Type:image/jpg');
header('Expires: access plus 30 days');
ob_clean();
flush();
readfile($filePath);
?>
<img src="http://localhost/prjt/getphoto/ZmLj" />
您需要添加
内容处置
标题:
header('Content-Disposition: attachement; ' .
' filename="yourimage.suf"; size="'2311"');
这将告诉浏览器打开具有给定文件名的下载对话框
接下来,将图像的数据流传输到客户端之后,您将不会呈现html标记。
而且,在将头放入缓冲区后,不应使用ob\u clean
擦除它们。
完整代码:
<?php
ob_clean();
header('Content-Description: File Transfer');
header('Access-Control-Allow-Origin:*');
header('Cache-Control:public, max-age=864000');
header('Content-Type:image/jpg');
header('Expires: access plus 30 days');
header('Content-Disposition: attachement; ' .
' filename="yourimage.suf"; size="'2311"');
readfile($filePath);
?>
// EOF !!
所以。。。扩展名丢失是的,它作为zmLj文件下载,图像显示在图像查看器中。。。。当用户尝试下载像zmLj.jpg这样的图像时,我如何添加类型?