Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save technoknol/5f41e3a7d157a1e85fc0af6d496b7af4 to your computer and use it in GitHub Desktop.
Save technoknol/5f41e3a7d157a1e85fc0af6d496b7af4 to your computer and use it in GitHub Desktop.
Php Stream - Write to temp stream and forcefully download file.
<?php
// Set the limit to 50 MB.
$fiveMBs = 50 * 1024 * 1024;
$fp = fopen("php://temp/maxmemory:$fiveMBs", 'r+');
fputs($fp, $couponString);
// Read what we have written.
rewind($fp);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . ('coupons.txt') . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
// header('Content-Length: ' . filesize($filepath));
flush(); // Flush system output buffer
echo stream_get_contents($fp);
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment