Forcing File "Save As" Downloads : header « HTML « PHP






Forcing File "Save As" Downloads

 
<?php
  $path = "images/winter.jpg";
  try {
    if (is_file ($path)){
      if ($file = fopen($path, 'rb')) {
        while(!feof($file) and (connection_status()==0)) {
          $f .= fread($file, 1024*8);
        }
        fclose($file);
      }
      $outputname = "myimage";
      header ("Content-type: image/jpeg");
      header("Content-disposition: attachment; filename=".$outputname.".jpg");
      print $f;
    } else {
      throw new exception ("Sorry, file path is not valid.");
    }
  } catch (exception $e){
    echo $e->getmessage();
  }
  
?>
  
  








Related examples in the same category

1.Changing the page type to CSV
2.Changing the page type to XML
3.Setting a Cookie Using the header() Function
4.Logged In with cookie
5.Good and bad Location headers
6.If a User Is Not Logged in, the Login Form Is Loaded
7.Mime types
8.Using header() to Send Raw Headers
9.Setting character encoding
10.Sending an XML response
11.Sending Content Types Other Than HTML
12.Reading Queued Headers
13.Redirect header
14.Redirecting with query string variables