SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict AndyH's Avatar
    Join Date
    Aug 2003
    Location
    Australia
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Force Download of an Image

    Hello,

    How would I force the downloading of an image via PHP - I guess it would use the header() function.

    A link would be www.example.com/download.php?image=1

    When clicked, the visitor would be prompted to download the image like what happens with .zip or .exe files.

    Thanks

  2. #2
    SitePoint Zealot shenkong's Avatar
    Join Date
    Sep 2004
    Location
    China
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just try:
    PHP Code:
    <?php
    $filename 
    "1.jpg";
    if (
    $fp = @ fopen($filename'r'))
    {
        
    header ("Content-type: octet/stream");
        if (
    strstr($_SERVER["HTTP_USER_AGENT"], "MSIE"))
        {
            
    header("Content-Disposition: filename=" $filename); // For IE
        
    }
        else
        {
            
    header("Content-Disposition: attachment; filename=" $filename); // For Other browsers
        
    }
        
        while (@ !
    feof($fp))
        {
            echo 
    fread($fp1024);
        }
    }
    ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •