Last active
March 11, 2017 06:58
-
-
Save zerolethanh/4755619d9e8bf9068d4f331cadf5caad to your computer and use it in GitHub Desktop.
saveBlob to public dir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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