File_get_contents shows unexpected output while reading a file

I want to output the embedded jpg image as a base64 encoded string, but when I do this:

$contents = file_get_contents($filename);
print "<img src=\"data:image/jpg;base64,".$contents."\"/>";

Where $filenameis the local text file with base64 image. The output is as follows:

<img src="data:image/jpg;base64,/9j/4A..... (the rest of the file)...." />

And frankly, the image is not displayed, but where does it come from ? It is not in a text file. When deleting, the image is displayed correctly.

+5
source share
1 answer

Unicode -. , , , UTF-8. , , , . . - , , , script :

print "<img src=\"data:image/jpeg;base64,".ltrim($contents, "\xEF\xBB\xBF")."\"/>";
+9

All Articles