TRUE, 'ignore' => array($dest))); } else if (is_file($src)) { $files = array($src); $base = dirname($src); } else { throw new InvalidArgumentException("Source '$src' is not a directory or file"); } if (is_dir($dest)) { $dest = CrayonUtil::path_slash($dest); $zipFile = $dest . basename($src) . '.zip'; } else if (is_file($dest)) { $zipFile = $dest; } else { throw new InvalidArgumentException("Destination '$dest' is not a directory or file"); } if ($removeExistingZip) { @unlink($zipFile); } $zip = new ZipArchive; if ($zip->open($zipFile, ZIPARCHIVE::CREATE) === TRUE) { foreach ($files as $file) { $relFile = str_replace($base, '', $file); $zip->addFile($file, $relFile); } $zip->close(); } else { throw new Exception("Could not create zip file at '$zipFile'"); } return $zipFile; } /** * Sends an email in html and plain encodings with a file attachment. * * @param array $args Arguments associative array * 'to' (string) * 'from' (string) * 'subject' (optional string) * 'message' (HTML string) * 'plain' (optional plain string) * 'file' (optional file path of the attachment) * @see http://webcheatsheet.com/php/send_email_text_html_attachment.php */ public static function emailFile($args) { $to = self::set_default($args['to']); $from = self::set_default($args['from']); $subject = self::set_default($args['subject'], ''); $message = self::set_default($args['message'], ''); $plain = self::set_default($args['plain'], ''); $file = self::set_default($args['file']); // MIME $random_hash = md5(date('r', time())); $boundaryMixed = 'PHP-mixed-' . $random_hash; $boundaryAlt = 'PHP-alt-' . $random_hash; $charset = 'UTF-8'; $bits = '8bit'; // Headers $headers = "MIME-Version: 1.0"; $headers .= "Reply-To: $to\r\n"; if ($from !== NULL) { $headers .= "From: $from\r\n"; } $headers .= "Content-Type: multipart/mixed; boundary=$boundaryMixed"; if ($file !== NULL) { $info = pathinfo($file); $filename = $info['filename']; $extension = $info['extension']; $contents = @file_get_contents($file); if ($contents === FALSE) { throw new Exception("File contents of '$file' could not be read"); } $chunks = chunk_split(base64_encode($contents)); $attachment = <<