title: php force download using header() broken downloaded file
body: <p>So firstly i would like to tell that i am quite newbie in header usage and playing with output buffers. So im developing php portlet for liferay and i have some problem with file downloads. I tried to do it simply with 'a href' but problem is that file uploaded via php is unavailable till apache is refreshed so i tried another way with header() function. </p>
  
  <p>So i will try to explain my problem. When i tried following code on simple php project it works fine :</p>
  
  <pre><code>    &lt;?php
  
  $path = "/mysecretdir/upload/"; // change the path to fit your websites document structure
  $fullPath = $path.$_GET['download_file'];
  
  if ($fd = fopen ($fullPath, "r")) {
  
      $fsize = filesize($fullPath);
  
      $path_parts = pathinfo($fullPath);
      $ext = strtolower($path_parts["extension"]);
  
      switch($ext) {
  
          case "pdf":
              $ctype = "application/pdf";
              break;
          case "exe":
              $ctype = "application/octet-stream";
              break;
          case "zip":
              $ctype = "application/zip";
              break;
          case "doc":
              $ctype = "application/msword";
              break;
          case "xls":
              $ctype = "application/vnd.ms-excel";
              break;
          case "ppt":
              $ctype = "application/vnd.ms-powerpoint";
              break;
          case "gif":
              $ctype = "image/gif";
              break;
          case "png":
              $ctype = "image/png";
              break;
          case "jpeg":
              $ctype = "image/jpg";
              break;
          case "jpg":
              $ctype = "image/jpg";
              break;
          case "mp3":
              $ctype = "audio/mp3";
              break;
          case "wav":
              $ctype = "audio/x-wav";
              break;
          case "wma":
              $ctype = "audio/x-wav";
              break;
          case "mpeg":
              $ctype = "video/mpeg";
              break;
          case "mpg":
              $ctype = "video/mpeg";
              break;
          case "mpe":
              $ctype = "video/mpeg";
              break;
          case "mov":
              $ctype = "video/quicktime";
              break;
          case "avi":
              $ctype = "video/x-msvideo";
              break;
          case "src":
              $ctype = "plain/text";
              break;
          default:
              $ctype = "application/force-download";
      }
  
      header("Pragma: public");
  
      header("Expires: 0");
  
      header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  
      header("Cache-Control: private",false);
  
      header("Content-type: " . $ctype);
  
      header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
  
      header("Content-Transfer-Encoding: binary");
  
      //header("Content-length: $fsize");
      header("Cache-control: public"); //use this to open files directly    
  
      while(!feof($fd)) {
          echo fread($fd, 4096); 
      flush();
      }
  }
  fclose ($fd);
  ?&gt;
  </code></pre>
  
  <p>from index.php im calling it with href="download.php?download_file=/something/" </p>
  
  <p>Ok but now the point. When i am using it in liferay portlet the file is weird. It puts whole generated HTML file in the created file. So its broken. I dunno why. I dont know if headers are sending some information from elsewhere and also dont know how to fix it. </p>
  
  <p>I was searching for hours for some solution but dont know how to make somehow "session" for headers, because i think there is problem with them. Becasue without echo it prints only content of file and in other project - more simple php app not as a part of portal it works! but maybe i am wrong.</p>
  
  <p>So please can someone help me? Any advise?</p>