Jump to content

Creating .zip Files On The Fly Using Lamp


Recommended Posts

Hey guys


I was wondering whether anyone knew of a way to automatically create .rar or .zip (I dont mind which) files from a list of files on my server...


A bit more info:


I want users to be able to select certain files and add them to a basket type thing. Then once they have paid I want a zip of the files they selected to be created on the fly and then emailed to them.


I know the emailing can be done, just not sure if its possible to do the compression thing dynamically.



Anyone know if this is do-able? Or anything similar?



Thanks chaps,


Link to comment
Share on other sites

There are a couple of ways to do it ( for example you could loop adding each file to the zip, or list them all in the single zip command ....


I haven't tested the code below, so there may be a couple of typos ;) but the principle should work


>$path= '/home/your-account/public_html/folder';  // Full path to folder of files 
$zipfile = "theirname.zip";									  // File name for their zip file 
$zippath = "/home/your-account/tempfolder";	  // Path to save the zip file
$to	  = "them@theirdomain.ext";			  // Who to send the emails to
$from	= "you@yourdomain.ext"; 			// Who should the emails be sent from?
$subject = "files for you from me"; 			// Subject in the email to be sent.
$message = "Your files are attached to this email"; 	// Brief Message.
$zipname = "$zippath/$zipfile";

// Create zip file ( zipping file1 and file2 ..... you can add more files here, or you could loop through all the file names ) 

passthru("nice -n 16 zip -q $zipname $path/$file1 $path/$file2");

// Create the attachment 

$fileatt_type = filetype($zipname);
$fileatt_name = $zipfile;
// Read the file to be attached 

$file = fopen($zipname,'rb');
$data = fread($file,filesize($zipname));

// Base64 encode the file data

$data = chunk_split(base64_encode($data));

// Generate a boundary string

$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// Add the headers for a file attachment

$headers = "From: $from\r\n";
$headers .= "To: $to\r\n";
$headers .= "MIME-Version: 1.0\n" ."Content-Type: multipart/mixed;\n" ." boundary=\"{$mime_boundary}\"";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: High\r\n";

// Add a multipart boundary above the plain message

$message = "This is a multi-part message in MIME format.\n\n" ."--{$mime_boundary}\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n" ."Content-Transfer-Encoding: 7bit\n\n";
$message .= "\n\n";
$message .= "--{$mime_boundary}\n" ."Content-Type: {$fileatt_type};\n" ." name=\"{$fileatt_name}\"\n";
$message .= "Content-Disposition: attachment;\n" ." filename=\"{$fileatt_name}\"\n" ."Content-Transfer-Encoding: base64\n\n";
$message .= $data . "\n\n" ."--{$mime_boundary}--\n";

// Send the message

@mail($to, $subject, $message, $headers. "-f" .$from);

// Remove zipfile 
exec("rm -r -f $zipname");

Edited by TCH-Andy
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...