Creating .zip Files On The Fly Using Lamp

Hey guys<br /><br />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...<br /><br />A bit more info:<br /><br />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.<br /><br />I know the emailing can be done, just not sure if its possible to do the compression thing dynamically.<br /><br /><br />Anyone know if this is do-able? Or anything similar?<br /><br /><br />Thanks chaps,<br />OJB<!--content-->
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 ....<br /><br />I haven't tested the code below, so there may be a couple of typos <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/wink.gif" style="vertical-align:middle" emoid=";)" border="0" alt="wink.gif" /> but the principle should work <br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->$path= '/home/your-account/public_html/folder';  // Full path to folder of files <br />$zipfile = "theirname.zip";                                      // File name for their zip file <br />$zippath = "/home/your-account/tempfolder";      // Path to save the zip file<br />$to      = "[email protected]";              // Who to send the emails to<br />$from    = "[email protected]";             // Who should the emails be sent from?<br />$subject = "files for you from me";             // Subject in the email to be sent.<br />$message = "Your files are attached to this email";     // Brief Message.<br />$zipname = "$zippath/$zipfile";<br /><br />// Create zip file ( zipping file1 and file2 ..... you can add more files here, or you could loop through all the file names ) <br /><br />passthru("nice -n 16 zip -q $zipname $path/$file1 $path/$file2");<br /><br />// Create the attachment <br /><br />$fileatt_type = filetype($zipname);<br />$fileatt_name = $zipfile;<br />        <br />// Read the file to be attached <br /><br />$file = fopen($zipname,'rb');<br />$data = fread($file,filesize($zipname));<br />fclose($file);<br /><br />// Base64 encode the file data<br /><br />$data = chunk_split(base64_encode($data));<br />    <br />// Generate a boundary string<br /><br />$semi_rand = md5(time());<br />$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";<br />    <br />// Add the headers for a file attachment<br /><br />$headers = "From: $from\r\n";<br />$headers .= "To: $to\r\n";<br />$headers .= "MIME-Version: 1.0\n" ."Content-Type: multipart/mixed;\n" ." boundary=\"{$mime_boundary}\"";<br />$headers .= "X-Priority: 1\r\n";<br />$headers .= "X-MSMail-Priority: High\r\n";<br /><br />// Add a multipart boundary above the plain message<br /><br />$message = "This is a multi-part message in MIME format.\n\n" ."--{$mime_boundary}\n";<br />$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n" ."Content-Transfer-Encoding: 7bit\n\n";<br />$message .= "\n\n";<br />$message .= "--{$mime_boundary}\n" ."Content-Type: {$fileatt_type};\n" ." name=\"{$fileatt_name}\"\n";<br />$message .= "Content-Disposition: attachment;\n" ." filename=\"{$fileatt_name}\"\n" ."Content-Transfer-Encoding: base64\n\n";<br />$message .= $data . "\n\n" ."--{$mime_boundary}--\n";<br />    <br />// Send the message<br /><br />@mail($to, $subject, $message, $headers. "-f" .$from);<br /><br />// Remove zipfile <br />exec("rm -r -f $zipname");<!--c2--></div><!--ec2--><!--content-->
WOW!<br /><br />hahah thanks a lot andy thats awesome.. really appreciate that...<br /><br />I will definitely give it a go at some point, thats brilliant!<br /><br /><br /> <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/tchrocks!.gif" style="vertical-align:middle" emoid=":tchrocks!:" border="0" alt="tchrocks!.gif" /> hahahah<!--content-->
You're welcome <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /> <br /><!--content-->
 
Back
Top