Page 1 of 1

Site design problem involving file downloads.

Posted: Fri Mar 14, 2003 7:45 pm
by Bill H
I have a client site which is built using PHP and MySQL and is located on a Unix server. The client creates assignments for the field staff by inputting to a database, and the staff logs on from their home computer to view the assignment and (later) to file reports.

Here is the problem I need to solve.

When a staff member logs on to receive his assignment he will need some MS Word documents which are stored on the server and need to be downloaded to his home computer. There are about 200 documents, 66 Kb to 220 Kb each, and a typical assignment will need about fifteen or so of them. (Different forms each time.)

I could list the documents as links and have him right-click on each one and then click "save as." But that is pretty ugly, and it's very error prone. Inevitably, the staff member will fail to download one or more forms.

(I probably could do something sexy with setting flags and try to not let him leave unless he's downloaded all of them, but that's "iffy" at best.)

The PHP header() function doesn't work because you can only send one header. That is, one file.

Using PHP ftp functions doesn't work, because those functions cannot access the machine on which the browser is running.

Zipping doesn't seem to present a solution, because it would have to happen without human intervention on the server.

What I want to do is after the staff member views the assignment is then "stream" the needed documents to a predetermined location on his computer automatically, and then display a message that says "Okay, you've got all the forms, go for it."

:?

Posted: Sat Mar 15, 2003 4:37 am
by volka
you might create a zip or tar via shellcommand and then stream the archive.
tar is present on almost all unix-systems and can be accessed with common win32-programs, too.
The programs zip and unzip are also bundled in many linux-distributions. If it is not present on your system but you want to use .zip you might find an apropriate package at http://www.info-zip.org/pub/infozip/UnZip.html

example code:

Code: Select all

<?php
// however this array is created...
$files = array('docs/1.doc', 'docs/2.doc', 'docs/3.doc');
$attName = 'assignmentDocs.zip';


$tmpName = uniqid('tmp', TRUE);
$cmd = 'zip -1Dj '.$tmpName.' '.join(' ', $files);

if (TRUE)
{
	exec($cmd);
	header('Content-type: application/zip');
	header('Content-disposition: attachment; filename='.$attName);
	header('Content-length: '.filesize($tmpName));
	readfile($tmpName);
	unlink($tmpName);
}
else
	echo $cmd; // debug
?>

Posted: Sat Mar 15, 2003 8:11 am
by Bill H
Thanks, I will put that in the idea bank.
My client wants a zip file about like he wants a bad case of the flu.
But needs may outweigh wants here.
:?

Posted: Sat Mar 15, 2003 8:58 am
by volka
you might also write a java-applet with a proper manifest that requests filesystem-access.
Can't think of another approach that isn't too tricky/risky...

Posted: Sat Mar 15, 2003 10:56 am
by Bill H
simplfy, Simplify, SIMPLIFY...
:)
"The forms you need have been emailed to you."
8)

Posted: Sat Mar 15, 2003 12:12 pm
by volka
:D

Posted: Sat Mar 15, 2003 2:29 pm
by McGruff
I haven't worked with files much in php but here's some random ideas which may or may not be helpful.

- attach the files to an email (if you can do that in php)
- concatenate groups of docs into a single file with file_get_contents()

It should also be possible to figure out something reasonably robust to make sure a full list of links are downloaded, like you suggested. It's much neater to have a single operation though, as you say.

Posted: Sat Mar 15, 2003 5:27 pm
by Bill H
- attach the files to an email (if you can do that in php)
Yep. see my post of Sat Mar 15, 2003 8:56 am
:)

Posted: Sat Mar 15, 2003 7:44 pm
by McGruff
Where did I put my glasses lol?