PHP supports the "data: stream wrapper" natively, so you don't have to do ugly string manipulation to get, for example, the data from a string such as data://text/plain;base64,SSBsb3ZlIFBIUAo= and then write it to a file. Instead, you can do this:

$string = 'data://text/plain;base64,SSBsb3ZlIFBIUAo=';
$source = fopen($string, 'r');
$destination = fopen('myfile.txt', 'w');

stream_copy_to_stream($source, $destination);

fclose($source);
fclose($destination);

You can also read the data into a string by simply doing this:

$data = file_get_contents('data://text/plain;base64,SSBsb3ZlIFBIUAo=');

Here is the relevant documentation, and you can see other supported wrappers (e.g. rar://).

Previous on PHP
Mastodon Mastodon