Most applications still aren’t uploading its files to S3, and this post is part of a series that tries to show the easiest possible way to implement this in different languages and technologies. Here we’ll show the simplest way in Php. For TL;DR version, just fork the repo and try it locally.
In order to use this implementation, we need to get the Amazon Php SDK, and you can get it easily via Composer (Don’t know what’s Composer? Stop everything you’re doing and read this site), creating composer.json file in the project root folder with the following content:
{ "require": { "aws/aws-sdk-php": "2.*" } }
After creating and modifying the config file, we need to download the dependencies, with this command:
php composer.phar install
Note that a vendor folder was created with AWS packages (in AWS SDK there are some extra libs accompanying Guzzle and Symfony Event Dispatcher).
Done with the dependencies, now it’s too easy. We have a very simple HTML form in the project with multipart/form-data encoding (index.php file), which submits to another file (upload.php) that receives form data and sends it straight to S3, without touching the local file system. We are using an existing bucket, but we could’ve also created a new bucket through the API.
Thus, we begin creating file index.php with the HTML form:
index.php
session_start(); require "vendor/autoload.php"; ?>Example Upload to S3 in PHP if (isset($_SESSION['msg'])): echo $_SESSION['msg']; endif; ?>Upload file to S3