alter code to download folders

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
dylan001
Forum Newbie
Posts: 16
Joined: Thu May 19, 2005 5:57 am

alter code to download folders

Post by dylan001 »

Hi,

I have come across a code for users to download files from my server and i'm trying to alter it so that it can either:

a) download multiple files in one go
b) download a folder containing multiple files

any help would be great.

Code: Select all

<?php

$filename = $_GET['file'];


if(ini_get('zlib.output_compression'))
  ini_set('zlib.output_compression', 'Off');


$file_extension = strtolower(substr(strrchr($filename,"."),1));

if( $filename == "" ) 
{
  echo "<html><title>ERROR</title><body>Error<body></html>";
  exit;
} elseif ( ! file_exists( $filename ) ) 
{
  echo "<html><title>ERROR</title><body>Error<body></html>";
  exit;
};
switch( $file_extension )
{
  case "pdf": $ctype="application/pdf"; break;
  case "exe": $ctype="application/octet-stream"; break;
  case "zip": $ctype="application/zip"; break;
  case "doc": $ctype="application/msword"; break;
  case "xls": $ctype="application/vnd.ms-excel"; break;
  case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
  case "gif": $ctype="image/gif"; break;
  case "png": $ctype="image/png"; break;
  case "jpeg":
  case "jpg": $ctype="image/jpg"; break;
  default: $ctype="application/force-download";
}
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers 
header("Content-Type: $ctype");
// change, added quotes to allow spaces in filenames, by Rajkumar Singh
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();

?>
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Whatever happened to *.zip files? :wink:

By the way, I'm sure a for loop will do what you're asking.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

You may only upload files, not directories.
RTFM Chapter 38. Handling file uploads, there is a specific example with multiple files.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

You can only download one file at a time.

Your choices are:
- ZIP (or other archive)
- bittorrent (create and start a torrent with exec(), then send the .torrent file)

Anyone else have a suggestion?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

hmm... what about multipart http response? How would a browser handle a response with multiple entities each having the 'Content-Disposition' header set to 'attachment'?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I've never seen nor heard of this behaviour, why not give it a try?!
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

Oh ... I feel stupid. Much more than I felt at 1:30am ;)
upload != download, even if it's past midnight ;)
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

Mordred wrote:Oh ... I feel stupid. Much more than I felt at 1:30am ;)
upload != download, even if it's past midnight ;)
It is if you were on the television...

(every show I've ever seen has reversed the meanings of up/download)
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

nickvd wrote:
Mordred wrote:Oh ... I feel stupid. Much more than I felt at 1:30am ;)
upload != download, even if it's past midnight ;)
It is if you were on the television...

(every show I've ever seen has reversed the meanings of up/download)
Well, even if they reversed them, it would still be true that upload != download :)

To say something constructive (?) on the topic, I seriously doubt that Weirdan's trick will work with the browser, and I also can't imagine how you would pass the additional entities' headers with PHP.

A stupid idea that might be usable is this:
redirect to a page which:
1. Sets a meta redirect after 3 seconds
2. Opens a popup that starts the download of the first file.
The meta refresh redirection is to a page that:
1. Sets a meta refresh redirect after 3 seconds
2. Opens a popup that starts the download of the second file
....
....
3. Profit ;)
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

Mordred wrote:Well, even if they reversed them, it would still be true that upload != download :)
Touché :D
Post Reply