Pages

Wednesday, January 16, 2013

Writing to downloadable file in PHP


$text_to_write = 'Example text to be written to file';
$fwrt = fopen($path.'file.txt', 'w');
fwrite($fwrt,$text_to_write);
fclose($fwrt);
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="example.txt"');
readfile($path.'file.txt');
exit();    // if this line is not included entire page code will also be written into the file

No comments: