Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save planetahuevo/e3f2e69b61408e14f4de8fe1de9e43ca to your computer and use it in GitHub Desktop.
Save planetahuevo/e3f2e69b61408e14f4de8fe1de9e43ca to your computer and use it in GitHub Desktop.
Upload to Amazon S3 Instead of Server for Sendy Email Image Uploads
//Using S3.php from https://github.com/tpyo/amazon-s3-php-class. Place it in includes/helpers
//Replaces Sendy's includes/create/upload.php
// for sendy 2.1.0.2
//Add in includes/config.php =>
// define('AWS_ACCESS_KEY', 'xxxx');
// define('AWS_SECRET_KEY', 'xxxx');
// define('S3_ENDPOINT', 's3.amazonaws.com');
// define('S3_BUCKET_NAME', 'xxxx');
<?php
include('../functions.php');
include('../login/auth.php');
require_once('../helpers/S3.php');
//Init
$file = $_FILES['upload']['tmp_name'];
$fileName = $_FILES['upload']['name'];
$extension_explode = explode('.', $fileName);
$extension = $extension_explode[count($extension_explode)-1];
$time = time();
$allowed = array("jpeg", "jpg", "gif", "png");
if(in_array($extension, $allowed)) {
$awsAccessKey = AWS_ACCESS_KEY;
$awsSecretKey = AWS_SECRET_KEY;
$bucketName = S3_BUCKET_NAME;
$s3 = new S3($awsAccessKey, $awsSecretKey, false, S3_ENDPOINT);
$s3Filename = 'sendy/'.$time.baseName($fileName);
if ($s3 -> putObject($s3->inputFile($file), $bucketName, $s3Filename, S3::ACL_PUBLIC_READ)) {
// Required: anonymous function reference number as explained above.
$funcNum = $_GET['CKEditorFuncNum'] ;
// Optional: instance name (might be used to load a specific configuration file or anything else).
$CKEditor = $_GET['CKEditor'] ;
// Optional: might be used to provide localized messages.
$langCode = $_GET['langCode'] ;
$url = 'http://'.S3_ENDPOINT.'/'.$bucketName.'/'.$s3Filename;
// Usually you will only assign something here if the file could not be uploaded.
$message = '';
echo "<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction($funcNum, '$url', '$message');</script>";
}
else exit;
}
else exit;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment