0

I have some images used for the avatars of the users on my website, for example http://drksde.tk/images/avatar-Luxie.jpeg I want to add a link that allow the users to download the avatar image, when someone click in the link a Save As dialog should appear.


Now the problem is that the picture is not downloaded properly, but the dialog appears, here is the link to download the avatar, and the code:

<?php
    $username = $_GET['username'];
    $size = $_GET['size'];
    $ext = $_GET['ext'];
    $border = $_GET['border'];
    $basename = basename($_SERVER['REQUEST_URI']);

    if(!isset($size)) { $size = 'small'; }
    if(!isset($ext)) { $ext = 'jpeg'; }
    if(!isset($border)) { $border = 'true'; }

$file = 'avatar-'.$size.'-'.$username.'.'.$ext;

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.$basename);
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    #header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
?>
5
  • 2
    Of course, they can always simply right-click on the image and "save as" Commented Mar 2, 2013 at 15:15
  • typically an avatar link would take you to the user's profile page. changing it to a "download the image" kind of breaks the point of having an avatar in the first place. I'd suggest leaving it as a link to a profile, then put an explicit "download this image" link on the profile page. Commented Mar 2, 2013 at 15:22
  • @mark-b I know, the site works like that, I'm asking about how to make the "download this image" link, to show an Save As dialog when the users clicks it Commented Mar 2, 2013 at 15:31
  • What have you tried so far? Have you any relevant code that you have problems with? Commented Mar 2, 2013 at 15:36
  • I don't tried anyhing, because I didn't found anything of help. 1. I have a picture, drksde.tk/images/avatar-Luxie.png for example. 2. I want to put a link on the website, something like "Download your avatar" that shows a Save As dialog when clicked, like when you try to download a file, but with a picture Commented Mar 2, 2013 at 15:39

1 Answer 1

1

You can check out Example #1 in the php.net manual here:

http://php.net/manual/en/function.readfile.php

It shows how to force an image download.

You could place it into a script image.php and use it like this

 <a href="http://example.com/image.php?user=123">download avatar image</a>

You could use it with the $_GET['user'] parameter to serve the right user image file.

Just remember to validate the GET input.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.