download file in php 1

download file in php

$filename = 'test.txt';

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($filename));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pregma: public');
header('Content-Length: '. filesize($filename));
readfile($filename);
exit();

Here is what the above code is Doing:
1. The Content-Type header tells the browser that the file is an octet-stream.
2. The Content-Disposition header tells the browser that the file is an attachment and the filename is test.txt.
3. The Content-Length header tells the browser the size of the file.
4. The readfile() function reads the file and writes it to the output buffer.
5. The exit() function terminates the script.

Similar Posts