Redirect Page after Download

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dayhuffb
Forum Newbie
Posts: 8
Joined: Thu Aug 12, 2004 5:45 pm

Redirect Page after Download

Post by dayhuffb »

I searched multiple forums for a good solution and have not found one. I want to download a file, then when the download has completed, refresh the mailbox to show that the file has been moved to an archive folder. The only solution I have come acrossed is to have a timed refresh of the main page, then set session variables in the download.php page which will get picked up by the refreshing page. A neat trick, but it requires that a main view in my site is constantly being refreshed whether the user is downloading or not. Are there any other methods of downloading files to a user using header tags that allow for web page redirection afterwards? Or non-header methods?

Thanks in advance,
Brad
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

AFAIK, there's no such header to tell the browser to redirect after the stream has finished..

You could, however, initiate the download from an already rendered page, and potentially redirect that page immediately after the download has started..
dayhuffb
Forum Newbie
Posts: 8
Joined: Thu Aug 12, 2004 5:45 pm

Post by dayhuffb »

I'm not sure if I'm completely understanding what your saying. How would I refresh a page after I have redirected to the download.php. Since both the downloading and redirect is handled in the header tag - and since nothing can be sent to the browser before the header tags, how would I go about doing this without having this refresh code always running in my mailbox.php page whether downloading or not?

Thanks Again,
Brad
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

user clicks download

browser changes to download page

in the download page, you have a javascript timer that calls a function to start the actual download. You also have a link for them to initiate the download directly (which should terminate the javascript timer also). In the javascript download start funciton, after opening a new window for the download, tell the current page to change locations to wherever you want them to be after the download is completed.
dayhuffb
Forum Newbie
Posts: 8
Joined: Thu Aug 12, 2004 5:45 pm

Post by dayhuffb »

feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color] 


I like the solution, but I am having a problem with implementing it.  If I click on the link, it works.  But when I open a new window using javascript, with the same link, the download window doesn't have the correct file information.  It says it is downloading a file called download.php instead of test.txt, and errors out when it cannot find the file.  I have seen many other posts on this topic, but none have been trying to open from a javascript window like myself (that I have found anyway).  I have attached a trimmed down version of my code if you need it.

-------- downloadmanager.php -----------------

Code: Select all

<a href="download.php">Click here</a>   //works
<script language="JavaScript">window.open('download.php','Download','width=300, height=300')</script>  //uses wrong file name in download
--------- download.php ----------------

Code: Select all

session_start(); 
if (!isset($_SESSION["status"])) { 
  echo "Error!";     
  exit; 
}        
$output_file = $_SESSION["downloadfile"];
global $HTTP_USER_AGENT; 

header("Content-Type: application/octet-stream"); 
header("Content-Type: application/force-download");  
// IE5.5 just downloads index.php if we don't do this 
if(preg_match("/MSIE 5.5/", $HTTP_USER_AGENT)) { 
  header("Content-Disposition: filename=$output_file"); 
}  
else { 
  header("Content-Disposition: attachment; filename=$output_file"); 
} 
header("Content-Transfer-Encoding: binary"); 

$fp = @fopen("ftp://".$_SESSION["FTPUSERNAME"].":".$_SESSION["FTPPASSWORD"]."@".$_SESSION["SERVERIP"].$_SESSION["USERFTPDIRECTORY"].$_SESSION["downloadfile"], "rb");
 while(!feof($fp)) {
     $buffer = fread($fp, 1024*6); //speed-limit 6kb/s
     if ($big_file>32 && 
     $extension!="jpg" && 
     $extension!="jpeg" && 
     $extension!="gif" && 
     $extension!="png" && 
     $extension!="txt")
     sleep(1); //if filesize>32kb and no smallfile like jpg,gif or so - wait 1 second
     print $buffer;
 }
 fclose($fp);
$_SESSION["status"] = "DOWNLOADED";
Once again, thank you very much for your help!

~Brad


feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
dayhuffb
Forum Newbie
Posts: 8
Joined: Thu Aug 12, 2004 5:45 pm

Post by dayhuffb »

The problem seems to be with using the session_start(); at the beggining. If I comment this line out, it works fine when calling a new window using javascript. Is this something I can get around? I don't understand why initializing a session would work if the page is accessed with a link, but would not work if opened in a new window with javascript?

Thanks Again,
Post Reply