How do I download a JPG file from the server? I have written a custom module in which i have a download button like given below.

$form['topics_info_graph']['download_graph'] = array(
 '#type'  =&gt; 'button',
 '#value' =&gt;  t('Download Graph'),
 '#button_type' =&gt; 'button',
 '#ajax' =&gt; array(
   'callback' =&gt; 'digitaliq_bckgrd_variable_ajax_download_callback', 
   'wrapper' =&gt; 'download_graph',
   'event' =&gt; 'click',
 ),
);


Onclick of this button i need to download a jpg image which is located in the temp folder.
I'm able identify the file name and the path, Not sure how to download. any help?

function digitaliq_bcgrd_variable_file_download($filename) {
  $temporary_directory = variable_get('file_public_path', conf_path().'/files').'/tmp';
  $temp_path = $temporary_directory . '/';
  // Download logic should come here, any help?
}
You can set the response headers from the PHP code to trigger a browser download of the file, e.g, something like:


function digitaliq_bcgrd_variable_file_download($filename) {
  $tmp = variable_get('file_public_path', conf_path().'/files').'/tmp';
  $filepath = $tmp . '/'. $filename;
  if ($data = file_get_contents($filepath)) {
    $file = new stdClass();
    $file-&gt;contents = $data;
    $file-&gt;filename = $filename;
    return EXAMPLE_serve_download($file);
  }
  drupal_not_found();
}


/**
 * Serve a file for download
 * @param $file
 * An object keyed with filename and contents
 */
function EXAMPLE_serve_download($file, $type = 'application/octet-stream') {

  if (empty($file-&gt;contents)) {
    drupal_not_found();
  }

  $headers = array(
    'Content-Type'              =&gt; $type,
    'Content-Disposition'       =&gt; 'attachment; filename="' . $file-&gt;filename . '"',
    'Content-Transfer-Encoding' =&gt; 'binary',
    'Pragma'                    =&gt; 'no-cache',
    'Cache-Control'             =&gt; 'must-revalidate, post-check=0, pre-check=0',
    'Expires'                   =&gt; '0',
    'Accept-Ranges'             =&gt; 'bytes'
  );
  if (ob_get_level()) {
    ob_end_clean();
  }
  foreach ($headers as $name =&gt; $value) {
    drupal_add_http_header($name, $value);
  }

  drupal_send_headers();
  print $file-&gt;contents;
  drupal_exit();

}