phphtmlregexhexdumphex-editors

Cannot find the ghost carriage return/line feed


I have a .php that file_get_contents of a openssl_pkcs7 signed and regex'd file, trimmed before echoing it for users to download.

However, the file downloaded has a line break/new line/carriage return at the top/start of the file which is breaking my subsequent code. The signed and regex'd template.mobileconfig doesn't have this empty line at the beginning of the file when I open it in notepad++/notepad but it's there when downloaded via a browser. I'm perplexed as to where it was inserted and even after trimming it, it is still there.

Code:

header("Content-type: text/plain");
header("Content-Disposition: attachment; filename=signed.mobileconfig");
$content = file_get_contents('./template.mobileconfig'); 
$trimmed = trim($content, " \t\n\r\0\x0B");
echo $trimmed;

The downloaded file looks like this in notepad/notepad++:

enter image description here

Line 1 is always empty. I've tried readfile() and trim() to try to work-around it but to no avail.

Output from hex editor online:

Output of a hex editor:

x0A apparently is a line feed but where is it coming from? Why didn't trim() get rid of it?

Line Feed x0A


Solution

  • It is possible one of your include()s has a newline \n:

    <?php
    //code
    ?>     <======== \n right there (not visible)
    

    Find all your included PHP files and remove spaces before the opening tag:

    _
    <?php 
    

    And get rid of the closing tag (not needed):

    ?>