470,833 Members | 1,352 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

readfile() and redirection

[PHP]
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);
}
}

}
[/PHP]

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:
[PHP]
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;
}
[/PHP]

This resulting from
index.php?forceDownload=1&fullFilePath=/tmp/images/blah.jpg
Thanx
Phil
Jul 17 '05 #1
3 8132
> 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.


Easy. Use an invisible frame to initiate the download.
Jul 17 '05 #2
"Chung Leong" <ch***********@hotmail.com> wrote in message news:<K8********************@comcast.com>...
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.


Easy. Use an invisible frame to initiate the download.


I'm sorry. Invisible frame? <Iframe>? I don't understand, I don't
follow your logic, I'm sorry.

What I have is that upon resizing the image it will do a header()
redirection to a URL that will have its headers changed using a class
method which allows for your browser window to remain in place while
the download prompt comes up via your browser to download the file.
After file download takes place you're still where you are and files
are managed.

I am interested in your easy solution, as you say, but can't
comprehend it in light of the modular approach I have done with the
application I've done so far. Please do explain "invisible frame" to
me.

Thanx
Phil
Jul 17 '05 #3
"Phil Powell" <so*****@erols.com> wrote in message
news:1c**************************@posting.google.c om...
What I have is that upon resizing the image it will do a header()
redirection to a URL that will have its headers changed using a class
method which allows for your browser window to remain in place while
the download prompt comes up via your browser to download the file.
After file download takes place you're still where you are and files
are managed.


It's very simple really. You want to both redirect to a HTML page and
initiate a download. We're talking two HTTP requests here. And the only way
you can initiate multiple page requests to the server with a single click
(without resorting to Javascript) is through frames.

In the page that you redirect to, stick in something like:

<iframe src="download.php?param=something" style="display: none" />

That will bring up a download box if the appropriate headers are set. In the
event of problems, the download script can output Javascript code that
either shows the errors on the parent page
(parent.document.getElementsById(...).innerHTML = '' ...) or redirect the
parent to an error page. An alternate strategy would have the page perform
the resizing, save the result to a temp file, which the download script
would then output and delete.
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by salsipius | last post: by
reply views Thread by Mustafa Ahmad Malik | last post: by
3 posts views Thread by Shawn August | last post: by
1 post views Thread by Jřrn Dahl-Stamnes | last post: by
4 posts views Thread by Eric Renken | last post: by
1 post views Thread by mkarja | last post: by
15 posts views Thread by Ketchup | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.