OJB Posted May 4, 2008 Posted May 4, 2008 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, OJB Quote
TCH-Andy Posted May 4, 2008 Posted May 4, 2008 (edited) 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)); fclose($file); // 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 May 4, 2008 by TCH-Andy Quote
OJB Posted May 4, 2008 Author Posted May 4, 2008 WOW! hahah thanks a lot andy thats awesome.. really appreciate that... I will definitely give it a go at some point, thats brilliant! hahahah Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.