How to make download an entire folder?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
boon4376
Forum Newbie
Posts: 19
Joined: Sun Oct 01, 2006 9:55 pm

How to make download an entire folder?

Post 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.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: How to make download an entire folder?

Post by Darhazer »

Create a zip and force downloading of that ZIP...
boon4376
Forum Newbie
Posts: 19
Joined: Sun Oct 01, 2006 9:55 pm

Re: How to make download an entire folder?

Post 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.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: How to make download an entire folder?

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to make download an entire folder?

Post 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.
boon4376
Forum Newbie
Posts: 19
Joined: Sun Oct 01, 2006 9:55 pm

Re: How to make download an entire folder?

Post 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.
Post Reply