Upload files and store in a database as a blog



This allows to let people upload images and store them in the database
Makes it easier to manage your website since all assets are stored inside the db
NOTE: This makes your database bigger so it is harder to quickly move it between locations

# 1 > EDIT page

-> Change the form to allow to upload images

-> add the input field that will accept files
<?php echo $this->Form->create('ModelName', array(
‘novalidate’ => true,
‘type’ => ‘file’ //Allows to upload images / data
));?>
<?= $this->Form->file('File'); ?>


# 2 > EDIT page - AppController

=> In AppController, add the following function

public function handleFiles($model, $imageColumn = ‘image’, $imageMimeColumn = ‘image_mime’) {
if (!empty($this->request->data[$model][‘File’][‘tmp_name’])) {
$this->request->data[$model][$imageColumn] = file_get_contents($this->request->data[$model][‘File’][‘tmp_name’]);
$this->request->data[$model][$imageMimeColumn] = $this->request->data[$model][‘File’][‘type’];
}


# 3 > Update Controller

=> Add the following function in the Controller

function admin_edit($id = FALSE){
   if (!empty($this->request->data)) {
   $this->handleFiles('ModelName');
   ….. other stuff now …..
   ….. save here ….
   }
}

=> Ensure your database has the fields image (type BLOB / LONG BLOB)
     image_mime (type TEXT 99)


# 4 > Update View

=> You can add the image to the admin_index in view, so you can see each image

<img src=“data:<?= $this->data[‘Model’][‘image_mime’]; ?>;base64,<?= base64_encode($this->data[‘Model’][‘image’]); ?>” height=“100">


Other Instructions

Below are many other instructions that show you how to use your UpdateCase application