Create Folder in AWS S3 Bucket Using PHP Code


To create folder in AWS S3 bucket, first you need is S3 connection object.
Now we will create S3 object:

try {
        $s3Client = S3Client::factory(array(
            'key'    => API_KEY,
            'secret' => API_SECRET
    } catch (S3Exception $e) {
        echo CONNECT_S3;

NOTE: We will put connection code in try catch to know the error if occurred.

Get Best Multi-Purpose WordPress Theme

Get Best WordPress Theme

Now we will create folder using following:

   'Bucket'       => BUCKET, // Defines name of Bucket
   'Key'          => "pictures/", //Defines Folder name
   'Body'       => "",
   'ACL'          => 'public-read' // Defines Permission to that folder

Here if there is no such folder in the given bucket, then it will create folder in it or else it will not.


Configuring AWS SDK with PHP

The AWS SDK for PHP can be configured in many ways to suit your needs. This guide highlights the use of configuration files with the service builder as well as individual client configuration options.

The simple way is as follows:

First we need to import AWS SDK  into our project and than we will do following,

require './AWS/aws-autoloader.php';

use Aws\S3\S3Client;
use Aws\Common\Exception\MultipartUploadException;
use Aws\S3\Model\MultipartUpload\UploadBuilder;
use Aws\S3\Exception\S3Exception; 

Now we will simply use below code to get connection with S3 Storage

try {
        $s3Client = S3Client::factory(array(
            'key'    => API_KEY,
            'secret' => API_SECRET
    } catch (S3Exception $e) {
        echo CONNECT_S3;

For more information you can visit AWS Configuration with PHP

Upload File to Amazon Cloud Server using AWS

To upload any file to amazon EC2  S3 cloud server using PHP code you simply need to include AWS package(SDK) and include necesary files into your code.

Below is the function to upload file directly to S3 Server:

function uploadFileToS3($file, $file_name){
// Here $file is a $_FILE array
// and $file_name is the name of file you need to create
try {
$s3Client = S3Client::factory(array(
‘key’ => API_KEY, // defines your API KEY of EC2
‘secret’ => API_SECRET // defines your API SECRET of EC2
} catch (S3Exception $e) {
echo CONNECT_S3;

$filename = “uploads/files/” . $file_name;
$uploader = UploadBuilder::newInstance()
->setMinPartSize(5 * 1024 * 1024)
->setOption( ‘ContentType’, $file[‘image’][‘type’])
->setOption(‘ACL’, ‘public-read’)
return true;


Now I will simply call the above function by passing 2 arguments i.e. $_FILES array and the file name which you need to create on S3 server.


$file_name = ‘my-new-image.png’;

$response = uploadFileToS3($_FILES, $file_name);

NOTE: Do not  forgot to include AWS package(SDK) on the top of your code.

You can download  from AWS Github

Implementing Caching in Core PHP

PHP Caching is used to Speed up Dynamically Generated Sites

Instead of regenerating the page every time, the scripts running this site generate it the first time they’re asked to, then store a copy of what they send back to your browser. The next time a visitor requests the same page, the script will know it’d already generated one recently, and simply send that to the browser without all the hassle of re-running database queries or searches.

Below is the overview of how cache works:



To achieve caching we can use many such as Memcache, Simple File that store entire page as cache etc.

We will be using the example of Cache Master. It is a user defined class with some functions to store and retrieve cache files.

For storing data in cache file we need to set:
1. Cache Directory
2. Expire Interval in seconds

Below is the code to how to use the same:



$someData = new stdClass();
$someData->oneItem = ‘One Item’;
$someData->twoItem = ‘Two Item’;
$someData->threeItem = ‘Three Item’;

$jsonEncodedData = json_encode($someData);
$key = ‘SomeUniqueId’;
$customDirectory = ‘/tmp/customDirectory/’;

* Cache it
$CachedMaster = new CacheMaster(); // user defined class
$CachedMaster->put($key, $jsonEncodedData);

* Load it
if( $CacheMaster->exists($key) )
$x = json_decode( $CacheMaster->get($key), true);
echo ‘<pre>’;
echo ‘</pre>’;



Download Cache Master file from here.

Sweet Alerts – Replacement of Javascript Alert Function

A beautiful replacement for  javascript’s “ALERT” is Sweet Alert( a.k.a Swal)

Sweet Alert is a responsive alert which automatically centers itself

on the page and looks great no matter if you’re using a desktop computer, mobile or tablet.

It’s even highly customizeable.

You just need to include 1 JS file and 1 CSS file for Sweet Alert i.e.

<script src="./js/sweet-alert.min.js"></script>
<link rel="stylesheet" href="./css/sweet-alert.css">

To call the sweetAlert-function you just need to do following:
   title: "Error!",
   text: "Here's my error message!",
   type: "error",
   confirmButtonText: "Cool" 

It is highly customize-able to get rid out of alert().

For downloading needed JS and CSS and documentation you can visit:
Sweet Alert Website

Connect SQLite3 Database with PHP

To connect SQLite Database(generally used in Mobile Devices) with PHP environment, then we simply need to do following:


//SQLite Class is provided my PHP itself, so no need to add any extension.

$db = new SQLite3(‘./db/employees_db.sqlite’); // Path to your SQLite DB

$results = $db->query(‘SELECT * FROM employees LIMIT 10’);
echo “<pre>”;
while ($row = $results->fetchArray(SQLITE3_ASSOC)) {