Using PHP to Download Multiple Files with One Click

Hi: I have a MySQL database that stores, for each record, the address of a file available for download. Using PHP, it’s quite easy to have a search utility that returns a hyperlink to the download file for each record that matches the search.

But there’s a twist! I need to give the user the option to click on a single link that will download ALL of the files that correspond to the matching records from the search if he so desires. Whether I use PHP to create an archive (Zip?) on the fly, or whether it would just be a succession of individual file downloads doesn’t really matter. But my client doesn’t want the user to have to click on each and every link if he chooses to download all of the files. Obviously I can’t archive them ahead of time, because the collection of files would be dependent upon the search criteria. Any ideas? :confused:

Thanks!

Well, actually it could be quite easily done.
Have your script output a series of frames, with each frame source pointing to the url of the file you want to be able to let users download. However, in this case, the downloads would appear quickly and the visitor have to accept each download, and if his computer isn’t strong enough to support the number of downloads that his query had sent, it might just crash his computer.

Just a way is to use frames anyway.

or refresh the page via meta for all the downloads.

Here is all I can find on zips, and it doesn’t include writing to them, just reading them.

I would look into zlib, it can be compiled into PHP, if that is an option, with it you can create compressed archives on the fly.

www.php.net/zlib

This way you can create an array of the filenames for download, create a compressed archive of the files, and then send the appropriate header information to allow the user to download the stream in one fast swoop.

Creating the ZIP file on the fly is pretty much the only solution. Even online file storage companies like the now-defunct iDrive would present a series of links for each file to be downloaded individually.

How can I do that? In researching PHP.net, it appears that I can’t write an actual zip file, I can only perform “read” functions on zip files. And I can use zlib to create gzip files on the fly, but a gzip archive can’t contain multiple files! (That last bit is from the Help within WinZip).

Is this not correct? :confused:

Hmm looks like you’re right. You may have to use PHP’s system or exec function to execute a zip program externally to compress the files…

Another option that I’m now trying is simply sending an e-mail with an attached file for each record in the search results. It’s not as good as having a single archive file, but in theory it would at least allow me to accomplish what I’m trying to do. I’m just replacing the concept of a download with an e-mail instead.

I’ve used PHP’s mail function before, but I’ve never attached a file. I tried some of the user-supplied suggestions from PHP.net, and I couldn’t get them to work. Is there an easy way to tell PHP to attach a file from the web server to the e-mail message that it sends out?

Thanks, as always, for your help! :smiley:

As far as I know you must go a either use, or make, a function that goes to send mail directly and talkwith it to attach files etc. Mail() doesn’t do that…as far as I know. There are some functions out there that can do this…try:

I am sure there is something there. Search for something like “mail and mime”.

Check out this if you haven’t already

assuming unix - you can use zip - gzip did not work properly for me when adding to an archive, so…

<?
$str=“file1.yak”;
system(“zip -r bigzip $str”);
?>

would create bigzip.zip, and imagining an array containing the selected downloads…

<?
for($x=0;$x<count($file_array);$x++){
system(“zip -r bigzip $file_array[$x]”);
}
echo “<a href=\“bigzip.zip\” target=\“blank\”>bigzip</a>”;
?>

note that system() echo’s returned info to the screen , if you do not want that use exec().