Page 1 of 1

alter code to download folders

Posted: Fri Feb 16, 2007 4:47 pm
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();

?>

Posted: Fri Feb 16, 2007 5:18 pm
by superdezign
Whatever happened to *.zip files? :wink:

By the way, I'm sure a for loop will do what you're asking.

Posted: Fri Feb 16, 2007 5:34 pm
by Mordred
You may only upload files, not directories.
RTFM Chapter 38. Handling file uploads, there is a specific example with multiple files.

Posted: Fri Feb 16, 2007 5:59 pm
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?

Posted: Fri Feb 16, 2007 6:06 pm
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'?

Posted: Fri Feb 16, 2007 6:14 pm
by Kieran Huggins
I've never seen nor heard of this behaviour, why not give it a try?!

Posted: Sat Feb 17, 2007 3:44 am
by Mordred
Oh ... I feel stupid. Much more than I felt at 1:30am ;)
upload != download, even if it's past midnight ;)

Posted: Sat Feb 17, 2007 8:51 am
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)

Posted: Sat Feb 17, 2007 9:04 am
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 ;)

Posted: Sat Feb 17, 2007 9:27 am
by nickvd
Mordred wrote:Well, even if they reversed them, it would still be true that upload != download :)
Touché :D