Page 1 of 1

download redirect

Posted: Fri Sep 23, 2005 8:38 am
by hame22
Hey all,

I have set up an application which allows the downloading of pdf files.

At present I have a page linking to the pdf download, when the user clicks the link a second browser opens and the download begins.

Code: Select all

if (file_exists($path)) 
	{

		$size = filesize($path);
		$file = basename($path);
		header ("Content-Type: application/octet-stream");
		header ("Content-Type: application/download");
		header("Content-Disposition: attachment; filename=$file");
		header("Content-Length: $size");
		readfile($path);
}
this works fine, however i want the page that contains this link to divert to another page once the download is complete. Is there any way of doing this??

thanks
alex

Posted: Fri Sep 23, 2005 8:44 am
by feyd
not really.. the browser doesn't tell you if and when the download finishes... You can send the main page there immediately after starting the download though..

Posted: Fri Sep 23, 2005 8:46 am
by hame22
yeah that wud do, how do i do that?

thanks

Posted: Fri Sep 23, 2005 8:52 am
by feyd
it requires Javascript but basically.. you launch your download link in whatever fashion probably needs to be a window.open() style call at this point.. then after making that call (and waiting a couple seconds for good measure) you change the document.location to somewhere else...

Posted: Fri Sep 23, 2005 8:56 am
by hame22
yeah i was contemplating that method, just wasnt too keen on using javascript, is there no other way to do this that you know of?

Posted: Fri Sep 23, 2005 8:59 am
by feyd
there are no other ways I'm aware of period.

Posted: Fri Sep 23, 2005 10:05 am
by hame22
Hi i have implemented the method the proble is now that the file cannot be found!! It works fine if i open the page via a form button, howver when i redirect to the page through the javascript the file is not found.

Here is my code:

Code: Select all

function load() {
var load = window.open('download.php?act_id=<?php print $act_id;?>');
}

on my page i use the following function to dowload the file:

Code: Select all

function download_file($product_code)
{
	$row = activity_query($product_code);
	$location = $row['location'];
	
	
	$path ='../../../../directory/'.$location.'';
	if (file_exists($path)) 
	{
		
		
		$size = filesize($path);
		$file = basename($path);
		
		header ("Content-Type: application/octet-stream");
		header ("Content-Type: application/download");
		header("Content-Disposition: attachment; filename=$file");
		header("Content-Length: $size");
		readfile($path);
		
		//update user's credits
		deduct_credits($product_code);
		
		//send confirmation email
		confirm_email($product_code);

	} 
	else
	{
		header("Location: $path");

	}
}

the pdf's are outside the root folder, cud this make a difference though it worked perfectly via the form button method, thanks

alex