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

前端 未结 3 725
无人共我
无人共我 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:08

    You can try to set the file name using the HTTP headers but not all browsers respect that.

    The simplest trick is to extend the URL so that the last part contains the desired file name:

    
    

    Note I also added the file name at the end of the query string (the name doesn't really matter) as some browsers use that part.

    Depending on your server configuration this will immediately work without any special configuration (no mod_rewrite or anything like that). You can check if it works on your server by simply appending "/foo" to any PHP-URL on your site. If you see the output of your PHP, all is good. If you see a 404 error then your server configuration can't deal with such URLs.

提交回复
热议问题