Nasrul Hazim Bin Mohamad

AWS: S3 – Upload File

Jan
15

Assalamualaikum / Hi,

Just a simple note on how to upload file(s) to AWS S3. First time working with AWS S3 – it’s not my account, but you get the idea how simple it is. ­čÖé

System Development: Must Have 3rd Party Library

Dec
22

A list of useful libraries used for system development – such as uploading, sorting, beautiful alert messages.

So, you don’t have to stick to those old schools methods for all the above. ­čÖé

ply

Ply, beautiful alert messages and even you can customize for login or registration modal.

DropzoneJS, similar to WordPress uploading Media.

DropzoneJS, similar to WordPress uploading Media.

sortable

Sortable, useful when it’s come to sort out your staff list for instance, or probably you have a list of products need to be sort.

CakePHP: Upload File

Apr
18

The Model

<?php

App::uses('AppModel', 'Model');

class Picture extends AppModel {
	public $useTable = 'picture';
	public $primaryKey = 'id';
	public $name = 'Picture';
	public $order = 'Picture.id ASC';

	protected $_displayFields = array(
		'id',
		'name',
		'link',
	);
}
?>

The Database

CREATE TABLE `picture` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `link` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

The View

<?php
	echo $this->Form->create('Upload',array(
		'enctype' => 'multipart/form-data',
		'url' => array(
			'controller' => 'upload', 
			'action' => 'index'
		)
	));
	echo $this->Form->file('file');
	echo $this->Form->end(array(
		'label' => 'Submit',
		'class' => 'btn btn-success'
	));
?>

The Controller

<?php

App::uses('AppController', 'Controller');

class UploadController extends AppController { 
	public function index() {
                // in this example, we're trying to accept gif,jpeg and png

                $image = $this->request->data['Upload']['file'];

                $imageTypes = array("image/gif", "image/jpeg", "image/png");

                $uploadFolder = "img" . DS . "photos" . DS;

                $uploadPath = WWW_ROOT . $uploadFolder;

                $id = null;

                if(!empty($image['name']))
                {
                        $ext = substr(strtolower(strrchr($image['name'], '.')), 1); //get the extension

                        if(in_array($image['type'], $imageTypes))
                        {
                                $date = date_create();
                                $rand = rand(100000,999999);
                                $link = 'pic_' . date_format($date, 'U') . '_' . $rand . '.' . $ext;

                                move_uploaded_file($image['tmp_name'], $uploadPath . $link);

                                $this->Picture->create();
                                $this->Picture->set(array('name' => $image['name'], 'link' => $link));
                                $this->Picture->save();
                                $id = $this->Picture->id;
                                $this->Session->setFlash('Picture uploaded!');
                        }
                        else
                        {
                                $this->Session->setFlash('Only files with extension .gif, .png and .jpeg are allowed to be upload.');
                        }
                }

	}
}
?>