Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php下载时通过readfile()函数呈现的图像缺少扩展名_Php_File Io_Content Type - Fatal编程技术网

使用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这样的图像时,我如何添加类型?