View Full Version : help please with dynamic file download problem
12-05-2005, 10: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
12-05-2005, 10: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
So you redirect to a thank you page.
12-05-2005, 11:13 PM
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:
And here is the script (dl.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
// Send the file contents.
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?
12-06-2005, 07: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.
12-06-2005, 07:58 PM
12-06-2005, 08:56 PM
Heres what you do
Before the headers() type:
window.open('thanks.php'); // or document.location
setTimeout('redir()', 500); // Adjust the time limit to suit your needs.
Powered by vBulletin® Version 4.2.2 Copyright © 2014 vBulletin Solutions, Inc. All rights reserved.