Dominicanstan
Dominicanstan

Reputation: 205

How do I ZIP selected files for user to download?

Its my first post in stackoverflow, so please let me know if something is wrong so I can fix it, thanks.

I have the following code in my website...

<table id="fruits">
    <tbody>
        <tr class="file" id="tr-file">
            <td class="name" id="id1"><a class="thickbox" href="FILE-LINK">NAME-OF-FILE1</a></td>
            <td class="name" id="id2"><a class="thickbox" href="FILE-LINK">NAME-OF-FILE2</a></td>
            <td class="name" id="id3"><a class="thickbox" href="FILE-LINK">NAME-OF-FILE3</a></td>
            <td class="name" id="id4"><a class="thickbox" href="FILE-LINK">NAME-OF-FILE4</a></td>
        </tr>
    </tbody> 
</table>

I know I can zip the files in cpanel or upload the files zipped and put it as a link in the page... But I want the visitor to choose which files they want to download, no force them to download all 4 files. So I searched in Google and found a useful post. (THE PROBLEM IS THAT I JUST KNOW THE BASIC OF HTML, I dont understand what he wrote) I want the visitor to select multiples files from <table id="fruits">.

Example: http://jsfiddle.net/dn3L7/

 <table id="fruits">
        <tbody>
            <tr class="file" id="tr-file">
               <input type"checkbox" id="idc1"> <td class="name" id="id1"><a class="thickbox" href="FILE-LINK">NAME-OF-FILE1</a></td>
               <input type"checkbox" id="idc2"> <td class="name" id="id2"><a class="thickbox" href="FILE-LINK">NAME-OF-FILE2</a></td>
               <input type"checkbox" id="idc3"> <td class="name" id="id3"><a class="thickbox" href="FILE-LINK">NAME-OF-FILE3</a></td>
               <input type"checkbox" id="idc4"> <input type"checkbox" id=""idc1>  <td class="name" id="id4"><a class="thickbox" href="FILE-LINK">NAME-OF-FILE4</a></td>
            </tr>
        </tbody> 
    </table>

When they finish checking the files they will be able to click a button to download the zip file.

Upvotes: 2

Views: 4053

Answers (1)

Samuel Liew
Samuel Liew

Reputation: 79123

you can take a look at ZipArchive, you would be able to create zips with that and let the user download it.

Cletus provide a really good answer there. I humbly copy his sample here

$files = array('readme.txt', 'test.html', 'image.gif');
$zip = new ZipArchive;
$zip->open('file.zip', ZipArchive::CREATE);
foreach ($files as $file) {
  $zip->addFile($file);
}
$zip->close();

and to stream it:

header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=filename.zip');
header('Content-Length: ' . filesize($zipfilename));
readfile($zipname);

Upvotes: 3

Related Questions