class DownloadGenerator {
var $fullFilePath;
function DownloadGenerator($fullFilePath) {
$this->fullFilePath = $fullFilePath;
}
function &generateForceDownloadHeaders($fullFilePath = '') { // STATIC VOID METHOD
$file = ($this->fullFilePath) ? $this->fullFilePath : $fullFilePath;
if (if_file($file)) {
$filesize = @filesize($file);
header("Content-Disposition: attachment; filename=$file");
header('Content-Type: application/octet-stream');
header('Content-Type: application/force-download');
header('Content-Type: application/download');
header('Content-Transfer-Encoding: binary');
header('Pragma: no-cache');
header('Expires: 0');
@set_time_limit(600);
readfile($file);
}
}
}
I am trying to "have my cake and eat it too", so to speak. I want to do a forced download of a file using readfile() and manipulating the headers; but at the exact same time I need to redirect to another page, the page that says "hey everything is cool" success message and display the rest of the application.
Right now what it does is that it does force a download (in Mozilla) but your window is blank. The user has no ability to navigate any further and is forced to close and re-open their browser and start all over again, definitely not an option.
How can I use readfile() to force a download of a file, yet at the same time display a screen indicating the rest of the application?
The idea I thought up was this:
foreach ($_REQUEST as $key => $val) if (!isset(${$key})) ${$key} = $val; // RETRIEVE ALL PASSED VARS
if ($forceDownload) { // HANDLE FORCED DOWNLOAD
$dlGen =& new DownloadGenerator($fullFilePath);
$dlGen->generateForceDownloadHeaders();
echo "<a href=\"index.php" . $dlGen->generateQueryString() . "\">Continue</a>\n";
$dlGen = null;
}
This resulting from
index.php?forceDownload=1&fullFilePath=/tmp/images/blah.jpg
Thanx
Phil