Hi everyone

Does anyone know of a PHP solution, or even a HTML solution, whereby a visitor could select a group of about 10 files (out of a couple of thousand!) and then click a "Download" button to download the selected files onto his/her PC?

There is one solution I've come across and that is to Zip the files first, but that would be far too complicated for my users.

Thanks in advance
Terry

Recommended Answers

All 7 Replies

Hi there,
Hope this helps. I havent tested it but it looks like something that may help.

Forcing a download

<?php
$file = 'monkey.gif';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?>

Refer to example 1 at:http://www.php.net/manual/en/function.readfile.php

Let me know if it works, may be able to use it myself in future!

Hi Setvir

I had a look at your code and the examples and whatever on your link, but unless I am missing something radical, I cannot see how that script would allow the simultaneous or sequential download of say 10 files?

Perhaps if you were to explain a bit more?

Terry

If you want one file downloaded with all the files in them the only way is to do it with a zip file (maybe other formats could be used but zip is standard).

You could offer an option where they can select the files. Then when they click download they are directed to another page. Each download file can then be started with javascript to start downloading by opening the php page (with the above script on) in a different window for each file to be downloaded.

<script>
  window.open('http://www.yourdomain.com/get_file,php?file=bluebird.jpg');
  window.open('http://www.yourdomain.com/get_file,php?file=nice_lady.jpg');
  window.open('http://www.yourdomain.com/get_file,php?file=chinesecrested.gif');
  etc..
</script>
if (file_exists($_GET['file'])) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}

But you may have problems with popup blockers(!?).


You could go completely different route and send the user an email with the files attached.

I think personally the first and last options are the things you should look at.

Member Avatar for diafol

I suggest you use an archiving solution, like zlib. Place all files into a zip file. Downloading multiple files individually is awkward to say the least. Placing said files into an archive should also ensure that all your downloads are in one place, easy to access. If you downloaded say 15 files to your downloads folder, it could all get very messy indeed.

Member Avatar for diafol

Eek! exe alert! Trust me I'm not a nasty piece of malware! Good if you're downloading your own files from your own site. If anybody else is doing the downloading... I'd delete it as soon as look at it.

Lol, true! But it is an option...

I would not download anything from any site I do not trust.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.