By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,521 Members | 1,407 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,521 IT Pros & Developers. It's quick & easy.

Script to download files only allows 1 download at a time?

P: 5
Hi,
I have a script which based on an id passed to the page is retrieving a filename and path from the database of a file and then outputting the file for download. I specifically did it this way (see the code below) so user would have a choice of "Saving" or "Running" the file.
The problem is that with this method when a file is clicked for download the user cant then select another file to download. It seems like this script prevents the server from allowing more than one download at a time. I dont know much about this, so any help would be really appreciated!!!
Thanks,
here is the code which prompts the download
[PHP]
switch($extlwr) {
case ($extlwr == ".zip"):
$commonname="zip";
$ct = "Content-type: application/octet-stream";
break;
case ($extlwr == ".mp3"):
$commonname="mp3";
$ct = "Content-type: audio/mp3";
break;
case ($extlwr == ".pdf"):
$commonname="pdf";
$ct = "Content-type: application/pdf";
break;
case ($extlwr == ".doc"):
$commonname="doc";
$ct = "Content-type: application/ms-word";
break;

}
$size = strval(filesize("$filepath$location"));

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("$ct");
header('Content-Disposition: attachment; filename="'.$filename.'";');
header("Content-Transfer-Encoding: binary");
if($extlwr != ".pdf"){
header('Content-Length: '.$size);
}
readfile("$filepath$location");
exit;
[/PHP]

any ideas?
thanks again
Feb 22 '07 #1
Share this Question
Share on Google+
10 Replies


P: 99
if you output the file to the browser the file is generated so there is nothing else to do but wait until the file is fully generated.

You could save the files somewhere in a cache directory and display obfuscated for the download.

you could also add

[PHP]header("Content-Type: application/octet-stream");[/PHP]

to the header to force the file to be downloaded.
Feb 22 '07 #2

P: 5
Thanks for your help
Even though I am outputting it to the browser, I am doing this in a new window. Why shoudl this stop the user from being able to click on another link in the original window to initiate a new download?

What does this mean?
"display obfuscated for the download."

Thanks again
Feb 22 '07 #3

P: 99
Thanks for your help
Even though I am outputting it to the browser, I am doing this in a new window. Why shoudl this stop the user from being able to click on another link in the original window to initiate a new download?

What does this mean?
"display obfuscated for the download."

Thanks again
I have no experience with it myself but have you tried a checkboxlist so that the user can choose which files he wants to download and then output the different files in new windows?


Sorry it had to be display an obfuscated link for the download. Lets say a file is in the directory /upload/nice.jpg. You put in the download link something like download.php?file=fe4564ds4. the file parameter can be created from the session id and the directory of filename mixed in it in a pattern you know how to get it out again. This way you don't have to generate the files everytime someone wants to download them and you are protected from hot linking.
Feb 22 '07 #4

P: 5
I have no experience with it myself but have you tried a checkboxlist so that the user can choose which files he wants to download and then output the different files in new windows?


Sorry it had to be display an obfuscated link for the download. Lets say a file is in the directory /upload/nice.jpg. You put in the download link something like download.php?file=fe4564ds4. the file parameter can be created from the session id and the directory of filename mixed in it in a pattern you know how to get it out again. This way you don't have to generate the files everytime someone wants to download them and you are protected from hot linking.
I am sorry, I am still not clear. Why do I need to be generating files at all? in fact what does it mean to be generating files?
I am using a download.php?fid=24 where the fid is the fileid in the database so it knows which file to pull. I just dont know why when I output the file to browser it wont allow the user to download a different file. If the user clicks a different file then only once the first download completes does the second window popup asking the user where to save the file? In fact more than that - the whole website slows down for the user and they cant even browse to other pages on the site while a file is being downloaded?? There is obviously something i am doing very wrong here?
thanks for your patience and help
Feb 22 '07 #5

P: 99
If you output the field content of the database it is one long string. To make it readable for humans the string is preceded by a header. this header tells the browser the file needs to be generated in that format. Then you add the content. So the file gets build from that string. Once it's finnished you see the image, can listen to the song, ...

it's like a string you manipulate with string functions. but now php does the hard work for you.

It is better to store the files in directories. This way no database connection is needed and the file already is in the human readable format.

Because it's such a long string if the file is large it takes up all the server memory and you can't do anything else until the string is read out.
Feb 22 '07 #6

P: 5
If you output the field content of the database it is one long string. To make it readable for humans the string is preceded by a header. this header tells the browser the file needs to be generated in that format. Then you add the content. So the file gets build from that string. Once it's finnished you see the image, can listen to the song, ...

it's like a string you manipulate with string functions. but now php does the hard work for you.

It is better to store the files in directories. This way no database connection is needed and the file already is in the human readable format.

Because it's such a long string if the file is large it takes up all the server memory and you can't do anything else until the string is read out.
So would the following work, I think its similar to what you were explaining before,
if someone clicks on a link a download script will copy the file from its source directory into a temporary directory with some temp filename and then serve that file to the user, not in the way I am serving it now with the headers and content but just by redirecting them to that file eg http://www.url.com/tempdir/tempfilename.mp3
Would that work? or do you have a better suggestion

my only issue is that I would need to delete these temp files and when would I know to delete them?
Feb 22 '07 #7

P: 99
So would the following work, I think its similar to what you were explaining before,
if someone clicks on a link a download script will copy the file from its source directory into a temporary directory with some temp filename and then serve that file to the user, not in the way I am serving it now with the headers and content but just by redirecting them to that file eg http://www.url.com/tempdir/tempfilename.mp3
Would that work? or do you have a better suggestion

my only issue is that I would need to delete these temp files and when would I know to delete them?
It's a way to go. To know how when to delete the picture you could put the date the file is clicked into a database table and temp file that are older then 30 minutes will be deleted.
Feb 22 '07 #8

P: 5
It's a way to go. To know how when to delete the picture you could put the date the file is clicked into a database table and temp file that are older then 30 minutes will be deleted.
actually I can just compare it to the time now, and more than 30 minutes delete it!
sounds good, thanks so much for your help!
Feb 22 '07 #9

P: 1
hey there!

I'm having the same problem. I'm using 2 servers though. One on which i have my php pages & one on which i put my mutlimedia...

I got the script to work, however, the site gets "stuck", until the download is complete...

Is there any way I could solve this problem please?
Sep 3 '07 #10

P: 6
hey there!

I'm having the same problem. I'm using 2 servers though. One on which i have my php pages & one on which i put my mutlimedia...

I got the script to work, however, the site gets "stuck", until the download is complete...

Is there any way I could solve this problem please?
I had this problem and it was a session header problem.
If you have "session_start();" at the top of the page where you're initiating the download, it keeps the session header in use thus not allowing any other header information to be sent/received from that session.

If you remove session_start(); from the top of the page, this should no longer be a problem.
Oct 14 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.