PDA

View Full Version : help please with dynamic file download problem



george
12-05-2005, 11:10 PM
Is the following possible:

1)User, who accesses my web site to download a file, clicks a "Download Now" button.

2)A script called by the button prompts the user to download the file (through the standard "Open or Save" dialog box or by any other means), AND THEN automatically redirects the user to a thank you page, with the user's only action being pressing the "Download Now" button.

The redirection can take place either before, or at the same time as the download is going on, or after the
download is completed.
Sending a sequence of header() to force the download AND present the Thank you page did not work for me.
thanks for any suggestions
george

execute
12-05-2005, 11:24 PM
You can probably do a href link to a download.php?f=filename
and then use
$_GET['f'] as the filename, and then use some headers to transfer file, WHICH will work, and then right after you read the file, do:
sleep(5); // if you want a delay
header("Location: Thanks.php");


So you redirect to a thank you page.

george
12-06-2005, 12:13 AM
Thank you Execute for your very prompt reply. What you suggest is exactly what I tried to do. Here is the page which calls the script:
<html><body><a href="http://localhost/PHPscripts/fileDownload/dl.php?filename=fileDownload.zip">
Download</a></body></html>

And here is the script (dl.php):
<?PHP
// File Download Script
// by James Warkentin
// This script is provided without warrenty of any kind, nor may the author be held liable for
// for any problems which arise as a result of the use of this script.
// Modify this line to indicate the location of the files you want people to be able to download
// This path must contain a trailing slash. ie. /temp/files/download/

$download_path = "c:/www/webroot/";
$filename = $_GET['filename'];

// Detect missing filename
if(!$filename) die("I'm sorry, you must specify a file name to download.");

// Make sure we can't download files above the current directory location.
if(eregi("\.\.", $filename)) die("I'm sorry, you may not download that file.");
$file = str_replace("..", "", $filename);
// Make sure we can't download .ht control files.
if(eregi("\.ht.+", $filename)) die("I'm sorry, you may not download that file.");
// Combine the download path and the filename to create the full path to the file.
$file = "$download_path$file";
// Test to ensure that the file exists.
if(!file_exists($file)) die("I'm sorry, the file doesn't seem to exist.");
// Extract the type of file which will be sent to the browser as a header
$type = filetype($file);
// Get a date and timestamp
$today = date("F j, Y, g:i a");
$time = time();
// Send file headers
header("Content-type: $type");
header("Content-Disposition: attachment;filename=$filename");
header("Content-Transfer-Encoding: binary");
header('Pragma: no-cache');
header('Expires: 0');
// Send the file contents.
set_time_limit(0);
readfile($file);
//
sleep(5);
header("Location: http://localhost/thankyou.html");
?>

If you remove the last two lines the download will work fine. But if you enable the last two lines the thankyou.html page shows up but no download takes place.
What am I doing wrong?
Thank you

nightdriver09
12-06-2005, 08:53 AM
Header info cant be changed anymore after the call to readfile() because the contents have already been output to the browser.

What you can do is to have an iframe which would handle the download, and I think it needs to interact with the parent too because as I see in your code, you are trying to output some error messages.

The iframe handles the download; the parent handles the error output and the redirection to the thankyou page. You need to do some javascript here. :)

george
12-06-2005, 08:58 PM
Thank you nightdriver09, you have pointed to the right direction. Preliminary testing suggests that javascript is not necessary if two iframes are used, one for the download, the other for the thank you page. The errors are displayed in the same window as the iframe causing the error (e.g. download file not found).
george

execute
12-06-2005, 09:56 PM
You could try using javascript, but you're right you can't change headers once they are sent, so i was wrong for giving you that advice.

Heres what you do
Before the headers() type:
echo "<script language='javascript'>
function redir(){
window.open('thanks.php'); // or document.location
}
setTimeout('redir()', 500); // Adjust the time limit to suit your needs.
</script>";