Used in conjunction with the Amazon AWS and the Comment Attachment plugin to retrieve all of a post’s attachments from amazon, zip them, and force download. Used on a project which tracks support tickets.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
//requires amazon-s3-and-cloudfront plugin function get_amazon_client($client_name = 's3'){ $plugin_path = ABSPATH . 'wp-content/plugins/amazon-s3-and-cloudfront/'; $AWS = new Amazon_Web_Services($plugin_path); $client = $AWS->get_client(); //returns an array of many different clients return $client[$client_name]; //return the requested client. Default Amazon s3 client. } // function to download all files tied to a ticket function download_aws_files($post_id = '') { global $post; if(!$post_id) { $post_id = $post->ID; } //get any uploaded attachments $attachments = get_posts( array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_parent' => $post->ID, 'exclude' => get_post_thumbnail_id() //don't get the featured image....not that there should be one. ) ); //shouldn't even be here if attachments are empty but check anyway if(! empty($attachments)){ // create the new zip file $zip = new ZipArchive(); $zip->open(($tmpfile = $post->ID.time().'.zip'), ZipArchive::CREATE); //documentation: docs.aws.amazon.com/aws-sdk-php/guide/latest/service-s3.html //further documentation: docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.S3.S3Client.html $client = get_amazon_client(); // get the amazon client so we can do stuff $bucket = 'mybucketname'; //basically a directory of our stuff...more accurately an object that Amazon understands $post_date = get_the_date('Y/m/'); $upload_dir = wp_upload_dir(); $dir = $upload_dir['basedir'].'/'.time().'/'; //make temp dir mkdir($dir, 0777); foreach ( $attachments as $attachment ) { $filename = end(explode('/', $attachment->guid)); $key = 'wp-content/uploads/'.$post_date.$filename; //make sure object exists or else we get a fatal error if($client->doesObjectExist($bucket, $key)){ // only get the objects we need by specifying the bucket and filename $result = $client->getObject(array( 'Bucket' => $bucket, 'Key' => $key )); $filepath = $dir.$filename; //lets save all files to a temp location. file_put_contents($filepath, $result['Body']); //now add them to the zip $zip->addFile($filepath, 'ticket-'.$post->ID.'-files/'.preg_replace('/^.*\//', '', $filename)); //tried using $zip->addFromString($filename, $result['Body']) but wasn't getting desired result. May come back to it. } } // save out the zip file $zip->close(); pk_rrmdir($dir); //remove all files and the directory itself. header('Content-Type: application/zip'); header('Content-Description: File Transfer'); header('Content-Disposition: attachment; filename=ticket-'.$post->ID.'-files.zip'); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: '.filesize($tmpfile)); while(ob_get_level()) ob_end_clean(); // serve up the file then delete (may cause issue with larger archives? hopefully not) readfile($tmpfile); unlink($tmpfile); exit(); } } |