How do I set the default 'save image as' name for an image generated in PHP?

前端 未结 3 748
无人共我
无人共我 2021-01-12 06:36

I have a page in my site, displaying some images that are produced my PHP. When I right click on an image and click Save Image As I get as default name the name of the php f

3条回答
  •  耶瑟儿~
    2021-01-12 07:01

    You can provide it in the Content-Disposition HTTP header:

    header('Content-Type: image/png');
    header('Content-Disposition: inline; filename="' . $filename . '"');
    

    However, some browsers (namely Internet Explorer) are likely to ignore this header. The most bullet-proof solution is to forge the URL and make the browser believe it's downloading a static file like /images/5/foo.png while the actual path behind the scenes is /picture_generator.php?image_id=5&extension=.png. This can be accomplished by some web server modules like Apache's mod_rewrite.

提交回复
热议问题