title: File download using PHP not working via HTTPS (works fine via HTTP)
body: <p>I'am being stuck for about a day on a strange problem (at least for me).</p>
  
  <p>I have many (usually PDF) files on the server's filesystem and I want to serve them to users through PHP file to make business logic easier (logging downloads, making the name of the file on users' filesystems nicer, ...)</p>
  
  <p>PHP code:</p>
  
  <pre><code>// $ext = extension of the file; 
  // $fullPath = full path of the file being downloaded on server's filesystem    
  
  switch($ext) { 
    case "pdf": $ctype="application/pdf"; break;
    case "zip": $ctype="application/zip"; break;
    case "doc": $ctype="application/msword"; break;
    case "xls": $ctype="application/vnd.ms-excel"; break;
    ...
    default: $ctype="application/octet-stream";
  }
  
  $fsize = filesize($fullPath);
  
  header("Pragma: public");
  header("Expires: 0");
  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  header("Cache-Control: private",false); // required for certain browsers
  header("Content-Type: " . $ctype);
  header("Content-Disposition: attachment; filename=\"" . $targetName . "\";");
  header("Content-Transfer-Encoding: binary");
  header("Content-Length: " . $fsize);
  ob_clean();
  flush();
  readfile($fullPath);
  </code></pre>
  
  <p><strong>Important:</strong> I use CloudFlare (free) so for HTTPS I use their SSL certificate (that works fine for everything else just fine)</p>
  
  <p><strong>And how it behaves?</strong> </p>
  
  <ul>
  <li>In case I call the script using HTTP everything works fine. </li>
  <li>When calling it using HTTPS it behaves strangely:
  
  <ul>
  <li>[ERROR] Safari (MacOS): the file is downloaded to the client's filesystem but browser doesn't finish the download,   </li>
  <li>[ERROR] Chrome 54 (MacOS): it looks like it even doesn't start,</li>
  <li>[ERROR] Chrome 51 (Win7 Enterprise + SP1): it looks like it downloads the file but at the end it says "Network error" and file cannot be opened,</li>
  <li>[OK] MSIE11 (Win7 Enterprise + SP1): works just fine</li>
  <li>[OK] curl in Terminal (MacOS): it works just fine and the file is downloaded and can be opened in the correct form</li>
  </ul></li>
  </ul>
  
  <p><strong>What I tried so far:</strong></p>
  
  <ul>
  <li>Various file sizes - same behaviour</li>
  <li>Two ISP (100Mbit optic fibre + 30/8Mbit Wireless) - same behaviour</li>
  <li>Setting Cloudflare to Development mode (so I should bypass the Cloudflare but SSL is still being used - so I think this could be the root cause of the problem) - no change in behaviour</li>
  <li>Added logging of certain "breakpoints" in the script - it runs even for HTTPS request as expected (no difference between HTTP and HTTPS request)</li>
  <li>search the web / StackOverflow but all answers should be already in the PHP script above (like "add Content-Disposition", "remove all white spaces") or try new "download" attribute for hyperlink HTML element (but it is still rather new and doesn't easily offer solution for "business needs")</li>
  </ul>
  
  <p><strong>Try by yourself</strong> (it is approx. 1,5MB file)</p>
  
  <ul>
  <li>HTTP: <a href="http://www.zatrolene-hry.cz/dialog/download-file?id_soubor=123" rel="nofollow noreferrer">http://www.zatrolene-hry.cz/dialog/download-file?id_soubor=123</a></li>
  <li>HTTPS: <a href="https://www.zatrolene-hry.cz/dialog/download-file?id_soubor=123" rel="nofollow noreferrer">https://www.zatrolene-hry.cz/dialog/download-file?id_soubor=123</a></li>
  </ul>