file_get_contents shows unexpected output while reading a file

前端 未结 1 1171
死守一世寂寞
死守一世寂寞 2021-01-13 09:54

I want to output an inline jpg image as a base64 encoded string, however when I do this :

$contents = file_get_contents($filename);
print \"

        
相关标签:
1条回答
  • 2021-01-13 10:41

    It's a Unicode Byte-Order Mark. The file was saved with an editor that added the BOM to indicate the file is encoded as UTF-8. So those bytes actually are in the file, but a text editor just won't show it since it's not text. For storing this kind of data you'll want to remove the BOM. Easiest way would be to configure your editor not to add the BOM, but if you don't have influence over the creation process of the file you could to it on-the-fly in your script too:

    print "<img src=\"data:image/jpeg;base64,".ltrim($contents, "\xEF\xBB\xBF")."\"/>";
    
    0 讨论(0)
提交回复
热议问题