download redirect

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
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

download redirect

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

Post by hame22 »

yeah that wud do, how do i do that?

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

Post 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...
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

there are no other ways I'm aware of period.
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

Post 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
Post Reply