Thanks croNix seems with code there is aways a 100 different ways of doing thing but your way seems the best.
I have updated my code and now written the last bit of the puzzle to create a random reference number, I did read that the random method I am using is not perfect but not sure give how its being used that I am not that worried (but if yo know of a better method please post)
Question is about security as i am allowing write access with this script should I add some sort of check to make sure the file is infact a png file ?
<?php
// report all errors for debug
ini_set('display_errors',1);
error_reporting(E_ALL);
// Get today's date so we can upload images to a directory with a date
$today = date('Y-m-j');
define('UPLOAD_DIR', 'images/' .$today. '/');
// Check if today's directory is there if not create
if (!is_dir(UPLOAD_DIR)) {
mkdir(UPLOAD_DIR);
}
// Handle the base64 upload and file naming
$img = $_POST['data'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$raw_file = UPLOAD_DIR . uniqid();
$file = $raw_file . '.png';
$success = file_put_contents($file, $data);
// Create a semi random reference string
$Ref = substr(md5(rand()),0,6);
// Print the Status and return a reference id
print $success ? $raw_file . "||" . $Ref : 'Unable to save the file.';
?>