Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 401295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:01:16+00:00 2026-05-12T17:01:16+00:00

This one should be easy, I think. I have a paginated image gallery, and

  • 0

This one should be easy, I think. I have a paginated image gallery, and under each image is a small link that says “Download Comp”. This should allow people to quickly download the .jpg file (with a PHP generated watermark) to their computer.

Now, I know I can just link straight to the .jpg file, but that requires the user to have the image open in a new window, right click, Save As…, etc. Instead, I want the “Download Comp” link to initiate the download of the file immediately.

PHP.net seemed to suggest using readfile(), so each “Download Comp” link is being echoed as “?download=true&g={$gallery}&i={$image}”.

Then at the top of the page I catch to see if the $_GET[‘download’] var isset, and if so, I run the following code:

if(isset($_GET['download'])) {
$gallery = $_GET['g'];
$image = $_GET['i'];
$file = "../watermark.php?src={$gallery}/images/{$image}";
header('Content-Description: File Transfer');
    header('Content-Type: application/jpeg');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: public');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
readfile($file);

}

The link takes a lonnnnnnnnng time, and then it brings up a dialog prompt asking you to Open or Save the file, but once you Save and try to open it, it says the file is corrupt and can’t be opened.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T17:01:16+00:00Added an answer on May 12, 2026 at 5:01 pm

    Don’t set $file to a relative url. The readfile function will try to access the php file on the server. That is not what you want. In your case it looks like the watermark.php file will send the contents you want, so you could possibly just set up the environment it needs and include it.

    <?php
    if(isset($_GET['download'])) {
        $gallery = $_GET['g'];
        $image = $_GET['i'];
        $_GET['src'] = "{$gallery}/images/{$image}";
    
        header('Content-Description: File Transfer');
        header('Content-Type: image/jpeg');
        header('Content-Disposition: attachment; filename='.basename($image));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: public');
        header('Pragma: public');
        ob_clean();
        include('../watermark.php');
        exit;
    }
    

    Another (simpler) way is to modify watermark.php. Add a query parameter to make it send the proper headers to force a download and link to that

    <a href="watermark.php?src=filename.jpg&download=true)">...</a>
    

    watermark.php:

    <?php
    if (isset($_GET['download']) && $_GET['download'] == 'true') {
        header('Content-Description: File Transfer');
        header('Content-Type: image/jpeg');
        header('Content-Disposition: attachment; filename='.basename($src));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: public');
        header('Pragma: public');
    }
    // continue with the rest of the file as-is
    

    Also, you don’t need the call to flush(). There should not be any output to send at that point, so it is not necessary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Should be an easy one. I thought, from reading this blog post that I
Its late and I probably should sleep on this. Easy one I have 3
This one should be easy. I just can't figure out what to search for...
This should be easy (at least no one else seems to be having a
This should be and easy one for the LINQ gurus out there. I'm doing
Let's say that some function returns object reference. How one should properly spell: this
This should be easy. I think I must be getting caught up on naming.
I think this should be easy, but it's evading me. I've got a many-to-many
I have a question that really feels like I should have an easy answer
This may not be the kind of question one should ask on StackOverflow, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.