www.webdeveloper.com
Results 1 to 8 of 8

Thread: force image to download not to display in browser

  1. #1
    Join Date
    Mar 2007
    Location
    Pakistan
    Posts
    75

    force image to download not to display in browser

    How can I force an image to be downloaded but not displayed in the browser window?

  2. #2
    Join Date
    Mar 2007
    Location
    Pakistan
    Posts
    75

    Question force image to download not to display in browser

    How can I force an image to be downloaded but not displayed in the browser window?

  3. #3
    Join Date
    Feb 2006
    Location
    I'm right here
    Posts
    103
    Put this in the head of your page.

    Code:
    <script type="text/javascript">
    var img1 = new Image();
    img1.src = 'path/to/your/image.gif';
    </script>
    This will cache the image in your temporary internet files folder, but won't display it until you want to.

  4. #4
    Join Date
    Sep 2006
    Location
    Europe
    Posts
    174
    Opps, bit of a bobo, meant to reply to the guy who was asking about file download in php. I have no idea how you force a download using JS.

    In PHP use header - use octet stream rather than file type (eg jpg)
    This together with content disposition will correctly force download for a document/image etc.

    Caution: absolutely do not allow the file to be choosen using $_GET['file'] without cleaning $_GET['file'] first.

    PHP Code:
            //set the content as octet-stream    
            
    header("Content-Type: application/octet-stream");    //
            // tell the thing the filesize
            
    header("Content-Length: " filesize($download_path.$file));    
            
    // set it as an attachment and give a file name
            
    header('Content-Disposition: attachment; filename='.$file);
            
    // read into the buffer
            
    readfile($download_path.$file); 
    Last edited by Taschen; 04-19-2007 at 03:35 AM. Reason: replied to wrong post

  5. #5
    Join Date
    Sep 2006
    Location
    Europe
    Posts
    174
    See you posted this under js as well. Here's the PHP reply.

    In PHP use header - use octet stream rather than file type (eg jpg)
    This together with content disposition will correctly force download for a document/image etc.

    Caution: absolutely do not allow the file to be choosen using $_GET['file'] without cleaning $_GET['file'] first.

    PHP Code:
            //set the content as octet-stream    
            
    header("Content-Type: application/octet-stream");    //
            // tell the thing the filesize
            
    header("Content-Length: " filesize($download_path.$file));    
            
    // set it as an attachment and give a file name
            
    header('Content-Disposition: attachment; filename='.$file);
            
    // read into the buffer
            
    readfile($download_path.$file); 

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,252
    This is what I use:

    download.php:
    PHP Code:
    <?php
    // Force download of image file specified in URL query string and which
    // is in the same directory as this script:
    if(!empty($_GET['img']))
    {
       
    $filename basename($_GET['img']); // don't accept other directories
       
    $size = @getimagesize($filename);
       
    $fp = @fopen($filename"rb");
       if (
    $size && $fp)
       {
          
    header("Content-type: {$size['mime']}");
          
    header("Content-Length: " filesize($filename));
          
    header("Content-Disposition: attachment; filename=$filename");
          
    header('Content-Transfer-Encoding: binary');
          
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
          
    fpassthru($fp);
          exit;
       }
    }
    header("HTTP/1.0 404 Not Found");
    ?>
    usage:
    HTML Code:
    <img src="/images/download.php?img=imagename.jpg" alt="test">
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    May 2004
    Location
    FL
    Posts
    3,447
    @haroon373,
    Don't double post. If you can't get an answer in one, state it and open a different one. The two have been merged.
    Lee

    ""Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."" -- Sir Winston Churchill

  8. #8
    Join Date
    Jun 2009
    Posts
    1

    Thumbs up Great code

    Hello,
    I used this code and it works great.

    http://gottweeters.com/follow-me-buttons

    Thanks NogDog!

    Quote Originally Posted by NogDog View Post
    This is what I use:

    download.php:
    PHP Code:
    <?php
    // Force download of image file specified in URL query string and which
    // is in the same directory as this script:
    if(!empty($_GET['img']))
    {
       
    $filename basename($_GET['img']); // don't accept other directories
       
    $size = @getimagesize($filename);
       
    $fp = @fopen($filename"rb");
       if (
    $size && $fp)
       {
          
    header("Content-type: {$size['mime']}");
          
    header("Content-Length: " filesize($filename));
          
    header("Content-Disposition: attachment; filename=$filename");
          
    header('Content-Transfer-Encoding: binary');
          
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
          
    fpassthru($fp);
          exit;
       }
    }
    header("HTTP/1.0 404 Not Found");
    ?>
    usage:
    HTML Code:
    <img src="/images/download.php?img=imagename.jpg" alt="test">

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles