Site design problem involving file downloads.

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
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Site design problem involving file downloads.

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

:?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
?>
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post 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.
:?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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...
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

simplfy, Simplify, SIMPLIFY...
:)
"The forms you need have been emailed to you."
8)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

:D
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post 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
:)
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Where did I put my glasses lol?
Post Reply