Skip to content

Instantly share code, notes, and snippets.

@rogeriotaques
Last active October 22, 2024 12:33
Show Gist options
  • Save rogeriotaques/03578d066dbe302b74f2915fbaaaa9dd to your computer and use it in GitHub Desktop.
Save rogeriotaques/03578d066dbe302b74f2915fbaaaa9dd to your computer and use it in GitHub Desktop.
Converts base64 string back to a pdf file
<?php
public function uploadFileFromBlobString($base64string = '', $file_name = '', $folder = '')
{
$file_path = "";
$result = 0;
// Convert blob (base64 string) back to PDF
if (!empty($base64string)) {
// Detects if there is base64 encoding header in the string.
// If so, it needs to be removed prior to saving the content to a phisical file.
if (strpos($base64string, ',') !== false) {
@list($encode, $base64string) = explode(',', $base64string);
}
$base64data = base64_decode($base64string, true);
$file_path = "{$folder}/{$file_name}";
// Return the number of bytes saved, or false on failure
$result = file_put_contents("{$this->_assets_path}/{$file_path}", $base64data);
}
return $result;
} // uploadFileFromBlobString
@assad25
Copy link

assad25 commented Nov 2, 2021

Thanks, sir you save my time. I am exploring solutions from almost 2 hours.

@rogeriotaques
Copy link
Author

I'm glad it has helped you! 🤗

@toddnguyengithub
Copy link

Thank you for helping me =))
I appreciate.

@rogeriotaques
Copy link
Author

My pleasure, @minhnq49!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment