Page 1 of 1

How to make download an entire folder?

Posted: Sun Jul 19, 2009 1:11 pm
by boon4376
I have images sorted into folders, and I want users to have the option of downloading the entire folder full of images to their computer.

Is this possible to do with PHP?

I've found some scripts to force download single files, but not entire folders full of files.

This is the file downloading script I have

Code: Select all

<?php
$file = "filename.ext";
 
// Quick check to verify that the file exists
if( !file_exists($file) ) die("File not found");
 
// Force the download
header("Content-Disposition: attachment; filename=\"" . basename($file) . "\"");
header("Content-Length: " . filesize($file));
header("Content-Type: application/octet-stream;");
readfile($file); 
?>
I have tried making the $file variable the name & location of the folder, ex:

Code: Select all

$file = "records/folder001";
I get a download with the proper name, however, it is 0 bytes in size.

Re: How to make download an entire folder?

Posted: Sun Jul 19, 2009 1:17 pm
by Darhazer
Create a zip and force downloading of that ZIP...

Re: How to make download an entire folder?

Posted: Sun Jul 19, 2009 2:00 pm
by boon4376
Darhazer wrote:Create a zip and force downloading of that ZIP...
I have over 5000 folders.... & dont want to maintain redundant zipped versions of folders as the contents of the folders may change

I'm looking for the simplest way to do this.... I'd prefer if there was just a download directory command...

IS there some php code that will zip a folder on the fly? then I could zip a folder, let the user download it, and have it removed automatically after a period of time.

Re: How to make download an entire folder?

Posted: Sun Jul 19, 2009 2:19 pm
by Darhazer
I mean exactly a PHP code, that makes a ZIP on the fly...
You may be interested in the zip extension or PclZip class

Re: How to make download an entire folder?

Posted: Sun Jul 19, 2009 3:44 pm
by requinix
boon4376 wrote:I'm looking for the simplest way to do this.... I'd prefer if there was just a download directory command...
There is not.

Re: How to make download an entire folder?

Posted: Sun Jul 19, 2009 4:29 pm
by boon4376
Darhazer wrote:I mean exactly a PHP code, that makes a ZIP on the fly...
You may be interested in the zip extension or PclZip class
Thanks Darhazer, PclZip was extremely easy to implement... Everything works great.