bfb 1985
bfb 1985

Reputation: 29

PHP not writing a Base64 image to html file

I have this string of data with some chords and their names:

$html="< img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAAA4AQMAAAB31mwxAAAABlBMVEX///8AAABVwtN+AAAAAXRSTlMAQObYZgAAAEVJREFUeF5jIApwgDCIEAFhEKEEwkoIEYQaLID/PxD8AMmDARpDBsaww8JAqMHUTsBkGRkow84Ok4FQQ5zJZDAG0GTCAADwOiM87WVzggAAAABJRU5ErkJggg==" alt="E" /> E

When I try to put this into a file

file_put_contents("data2.html",$html);

I just creates an empty file.

Upvotes: 0

Views: 261

Answers (1)

aborted
aborted

Reputation: 4541

Change your $html variable to this:

$html = '<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAAA4AQMAAAB31mwxAAAABlBMVEX///8AAABVwtN+AAAAAXRSTlMAQObYZgAAAEVJREFUeF5jIApwgDCIEAFhEKEEwkoIEYQaLID/PxD8AMmDARpDBsaww8JAqMHUTsBkGRkow84Ok4FQQ5zJZDAG0GTCAADwOiM87WVzggAAAABJRU5ErkJggg==" alt="E" /> E';

And then do the file_put_contents('data2.html', $html); part.

What we did here was replace the double quotes with single quotes so you wouldn't need to escape the double quotes inside the string. I also put an end with a semicolon to your string, which was missing. Always remember to end a statement when you start it.

Upvotes: 3

Related Questions