PHP Force downloding shows network error

force-download, php

Solution

This:

header("Content-Length: ".filesize($path) ."\"");

outputs in headers this:

Content-Length:"

replace by:

header("Content-Length: ".filesize($path));

and fix the `$path` which is also not correct because it returns `null` (or `false`).

The output should be something like:

Content-Length: 10032219

Also, the correct mime-type for PDF is:

 Content-Type: application/pdf

Problem

I trying to make document download using PHP force downloading and i used the below code ``` header("Content-Type: application/octet-stream"); header("Content-Transfer-Encoding: Binary"); header("Content-Length: ".filesize($path) ."\""); header("Content-Disposition: attachment; filename=\"".basename($path)."\""); header("Expires: 0"); header("Cache-Control: no-cache, must-revalidate"); header("Pragma: no-cache"); ob_clean(); flush(); readfile($path); exit; ``` But when i run the script it just shows download starting... and shows "Network error" in chrome and when i checked network console it shows something like this ``` Request URL:http://websddress.com/presse/index.php?option=com_presse&task=viewDoc&id=2578&lg=FR Request Method:GET Status Code:200 OK Request Headersview source Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Accept-Encoding:gzip,deflate,sdch Accept-Language:en-GB,en-US;q=0.8,en;q=0.6 Connection:keep-alive Cookie:__utma=139058359.696419680.1380560986.1380711878.1380731571.5; __utmz=139058359.1380560986.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); 58a32855c678fbf6131983dab57747c9=uv0bipblgn8bd3diqbd52dn9g1; 64cf32705845a188b511407ba5bcd3d6=edbgk8jtalamqnininr89qb327 Host:5.135.115.74 Referer:http:webaddress.com/presse/index.php?option=com_presse&task=viewDocs&secid=787&lg=fr User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36 Query String Parametersview sourceview URL encoded option:com_presse task:viewDoc id:2578 lg:FR Response Headersview source Cache-Control:no-cache, must-revalidate Content-Disposition:attachment; filename="Advanced_Fair_-_Grohe_september_doc_FR_2578.pdf" Content-Length:" Content-Transfer-Encoding:Binary Content-Type:application/octet-stream Date:Thu, 21 Nov 2013 15:49:29 GMT Expires:0 P3P:CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM" Pragma:no-cache Server:Microsoft-IIS/7.5 X-Powered-By:ASP.NET X-Powered-By:PHP/5.3.23 ```

Original source