title: Php function to save a image
body: <p>I have some images used for the avatars of the users on my website, for example <a href="http://drksde.tk/images/avatar-Luxie.jpeg" rel="nofollow">http://drksde.tk/images/avatar-Luxie.jpeg</a>
  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.</p>
  
  <hr>
  
  <p>Now the problem is that the picture is not downloaded properly, but the dialog appears, here is the link to <a href="http://drksde.tk/images/user-Luxie.jpeg" rel="nofollow">download the avatar</a>, and the code:</p>
  
  <pre><code>&lt;?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;
  ?&gt;
  </code></pre>