How to Automatically Start a Download in PHP?

What code do you require to include PHP to instantly have the internet browser download a documents to the neighborhood equipment when a link is seen?

I am especially considering capability comparable to that of download websites that motivate the customer to conserve a documents to disk as soon as you click the name of the software program?

2022-06-07 14:37:29
Source Share
Answers: 2

Send the adhering to headers prior to outputting the documents:

header("Content-Disposition: attachment; filename=\"" . basename($File) . "\"");
header("Content-Type: application/octet-stream");
header("Content-Length: " . filesize($File));
header("Connection: close");

@grom: Interesting concerning the 'application/octet - stream' MIME type. I had not been knowledgeable about that, have actually constantly simply made use of 'application/force - download'

2022-06-07 16:24:02

Here is an instance of returning a pdf.

header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
header('Content-Transfer-Encoding: binary');

@Swish I really did not locate application/force - download web content type to do anything various (examined in IE and also Firefox). Exists a factor for not returning the real MIME type?

Additionally in the PHP hand-operated Hayley Watson uploaded:

If you desire to compel a documents to be downloaded and install and also conserved, as opposed to being provided, bear in mind that there is no such MIME type as "application/force - download". The proper type to make use of in this scenario is "application/octet - stream", and also making use of anything else is just relying upon the reality that customers are intended to overlook unrecognised MIME kinds and also make use of "application/octet - stream" rather (reference: Sections 4.1.4 and also 4.5.1 of RFC 2046).

Additionally according IANA there is no licensed application/force - download type.

2022-06-07 15:03:17