codemirrorfile-put-contents

file_put_content adds empty lines in file


I am using CodeMirror to edit files in my application. To save the file I put the textarea-content in a form. Then I use

file_put_contents($filepath, $POST['textarea_name']):

This works fine and the file looks great in CodeMirror too. But when I take a look at the real files there are many empty lines added.

Example (dummy code):

<?php

if ( a > f) {
    echo 'test';
}

function test() {

}

?>

becomes:

<?php



if ( a > f) {

    echo 'test';

}



function test() {



}



?>

Does anyone has any idea where these lines are coming from?


Solution

  • Seems like I found a solution:

    The files where saved with Windows EOL (CRLF) instead of Unix (LF).

    str_replace(array("\r\n", "\r"), "\n", $filecontent );
    

    This should fix the issue. It did work for me.

    Credit: https://github.com/joomla/joomla-cms/pull/9616