0

I have some html files stored in AWS S3, and I access them with AWS CloudFront.

I need to call them from my PHP code, like that:

echo file_get_contents('https://mycdnurl.com/myhtmlfile.html');

It works, but it ignores all line breaks and sometimes it breaks the code because of some piece of javascript code inside the html like that:

// some javascript comment
var test = test

Turns into:

// some javascript comment var test = test

If I access the file directly from browser, it works nice. But I need to call from PHP without iframe.

I tried to search but I didn't find anything useful.

dm707
  • 352
  • 2
  • 15

1 Answers1

0

This does not sound right since file_get_contents() preserves the file content including newlines.

  • Perhaps your file contains the wrong type of line-breaks, e.g. Unix, Windows, Mac?

  • There might be an issue with the encoding as well (I doubt it). Try loading the file contents as UTF-8 with a fallback to your other encoding, e.g. Latin 1, if applicable.

    function file_get_contents_utf8($fn) {
        $content = file_get_contents($fn);
        return mb_convert_encoding($content, 'UTF-8',
            mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true));
    }
    
  • If nothing works, you could try cURL instead of file_get_contents(). Doc, example.

wp78de
  • 18,207
  • 7
  • 43
  • 71