Skip to content

Instantly share code, notes, and snippets.

@zerolethanh
Last active March 11, 2017 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zerolethanh/4755619d9e8bf9068d4f331cadf5caad to your computer and use it in GitHub Desktop.
Save zerolethanh/4755619d9e8bf9068d4f331cadf5caad to your computer and use it in GitHub Desktop.
saveBlob to public dir
<?php
if (!function_exists('saveBlob')) {
function saveBlob($blobdata = 'blobdata', $dir = 'blobdata')
{
//$blobdata head : data:image/jpeg;base64,
$blobdata = request($blobdata);
list($type, $blobdata) = explode(';', $blobdata);
list(, $blobdata) = explode(',', $blobdata);
$blobdata = base64_decode($blobdata);
$download_key = \Faker\Provider\Uuid::uuid() . date('_Y_m_d');
$fileExtension = last(explode('/', $type));
if (!is_dir($dir) || !is_dir(public_path($dir))) {
$dir = public_path($dir);
mkdir($dir, 0777, true);
}
$save_to = "{$dir}/{$download_key}.{$fileExtension}";
$write = file_put_contents($save_to, $blobdata);
if ($write) {
return [
'saved' => true,
'download_url' => url("/{$save_to}")
];
}
return [
'saved' => false,
'err' => true,
'err_message' => 'can not save to ' . $save_to
];
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment