-
-
Save planetahuevo/2e6a81bc3dc635ba9e8f6caa00a8b2bd to your computer and use it in GitHub Desktop.
Replacement for sendy's upload.php that sends files to S3 rather than the server.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(E_ALL); | |
ini_set('display_errors', 1); | |
include('../functions.php'); | |
include('../login/auth.php'); | |
require_once('../helpers/S3.php'); | |
if (!isset($_FILES['upload'])) { | |
die('No file uploaded'); | |
} | |
// Init | |
$file = $_FILES['upload']['tmp_name']; | |
$fileName = $_FILES['upload']['name']; | |
$uploadError = $_FILES['upload']['error']; | |
if ($uploadError !== UPLOAD_ERR_OK) { | |
switch ($uploadError) { | |
case UPLOAD_ERR_INI_SIZE: | |
$message = "The uploaded file exceeds the upload_max_filesize directive in php.ini."; | |
break; | |
case UPLOAD_ERR_FORM_SIZE: | |
$message = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form."; | |
break; | |
case UPLOAD_ERR_PARTIAL: | |
$message = "The uploaded file was only partially uploaded."; | |
break; | |
case UPLOAD_ERR_NO_FILE: | |
$message = "No file was uploaded."; | |
break; | |
case UPLOAD_ERR_NO_TMP_DIR: | |
$message = "Missing a temporary folder."; | |
break; | |
case UPLOAD_ERR_CANT_WRITE: | |
$message = "Failed to write file to disk."; | |
break; | |
case UPLOAD_ERR_EXTENSION: | |
$message = "File upload stopped by extension."; | |
break; | |
default: | |
$message = "Unknown upload error."; | |
break; | |
} | |
die($message); | |
} | |
$extension_explode = explode('.', $fileName); | |
$extension = end($extension_explode); | |
$time = time(); | |
$allowed = array("jpeg", "jpg", "gif", "png"); | |
if (!in_array($extension, $allowed)) { | |
die('File not allowed'); | |
} | |
$awsAccessKey = get_app_info('s3_key'); | |
$awsSecretKey = get_app_info('s3_secret'); | |
if (!$awsAccessKey || !$awsSecretKey) { | |
die('AWS credentials are missing'); | |
} | |
$bucketName = 'YOUR_BUCKET_NAME'; //change to your bucket name | |
$endpoint = 's3.us-east-1.amazonaws.com'; //change as appropriate | |
try { | |
$s3 = new S3($awsAccessKey, $awsSecretKey, false, $endpoint); | |
} catch (Exception $e) { | |
die('Failed to initialize S3: ' . $e->getMessage()); | |
} | |
$s3Filename = 'images/' . $time . basename($fileName); //added path to images folder on S3 bucket | |
// Additional debugging | |
echo "<script type='text/javascript'>console.log('Attempting to upload to S3: $s3Filename');</script>"; | |
try { | |
if (!file_exists($file)) { | |
throw new Exception('Temporary file does not exist'); | |
} | |
if (filesize($file) === 0) { | |
throw new Exception('Temporary file is empty'); | |
} | |
// Logging file details for debugging | |
echo "<script type='text/javascript'>console.log('File exists: " . file_exists($file) . "');</script>"; | |
echo "<script type='text/javascript'>console.log('File size: " . filesize($file) . "');</script>"; | |
$inputFile = $s3->inputFile($file); | |
if (!$inputFile) { | |
throw new Exception('Unable to create input file for S3 upload'); | |
} | |
// Logging input file details | |
echo "<script type='text/javascript'>console.log('Input file: " . json_encode($inputFile) . "');</script>"; | |
$result = $s3->putObject($inputFile, $bucketName, $s3Filename); | |
if ($result) { | |
$fileUrl = 'https://' . $bucketName . '.' . $endpoint . '/' . $s3Filename; | |
// Additional debugging | |
echo "<script type='text/javascript'>console.log('File uploaded successfully. URL: $fileUrl');</script>"; | |
$funcNum = $_GET['CKEditorFuncNum']; | |
$CKEditor = $_GET['CKEditor']; | |
$langCode = $_GET['langCode']; | |
$message = ''; | |
echo "<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction($funcNum, '$fileUrl', '$message');</script>"; | |
} else { | |
throw new Exception('S3 upload failed'); | |
} | |
} catch (Exception $e) { | |
$errorMessage = $e->getMessage(); | |
error_log('Failed to upload to S3: ' . $errorMessage); | |
echo "<script type='text/javascript'>console.log('Error: $errorMessage');</script>"; | |
die('Failed to upload to S3: ' . $errorMessage); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment