Skip to content

Instantly share code, notes, and snippets.

@bhowe
Created January 9, 2015 01:21
Show Gist options
  • Select an option

  • Save bhowe/235692424f9ad77d0ef2 to your computer and use it in GitHub Desktop.

Select an option

Save bhowe/235692424f9ad77d0ef2 to your computer and use it in GitHub Desktop.
Upload files to amazon s3 with php.
<?php
//This file has been modifed from running code if you have any problems contact info@wiseguystechnologies.com
//no direct access
if (!isset($_POST['sneaky'])) die("DIRECT ACCCESS NOT PERMITTED");
error_reporting(E_ALL);
ini_set('display_errors', '1');
$msg = '';
#download libs here http://aws.amazon.com/sdk-for-php/
require '/aws/aws-autoloader.php';
use Aws\Common\Aws;
$aws = Aws::factory(get_creditentials());
$s3 = $aws->get('S3');
if($_SERVER['REQUEST_METHOD'] == "POST"){
$name = ($_FILES['file']['name']);
$tmp = $_FILES['file']['tmp_name'];
$ext = getExtension($name);
if ($ext !== 'pdf') {
report('Not a PDF');
}
$source_file = file_get_contents($tmp);
$response = $s3->putObject(array(
'Bucket' => 'mycoolbucket',
'ACL' => 'public-read',
'Key' => $name,
'Body' => $source_file
));
report("success");
}
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = strtolower(substr($str,$i+1,$l));
return $ext;
}
function report($msg){
header('Location: whereever/?msg=' . urlencode($msg));
exit;
}
function get_creditentials()
{
return array(
'includes' => array('_aws'),
'services' => array(
'default_settings' => array(
'params' => array(
'key' => 'MY KEY',
'secret' => 'MY SECRET'
)
)
)
);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment