Ryan Farmer
Ryan Farmer

Reputation: 11

Uploading php files via FTP, all linebreaks and CRs are lost upon upload

I can download files I have no uploaded and carriage returns are in tact however upon upload my files are placed on a single line.

This is making it difficult to troubleshoot this app as there is an error but everything is on line 1.

Why is this happening?

Upvotes: 1

Views: 2347

Answers (2)

Avatar
Avatar

Reputation: 15186

Solution that worked for me:

  1. Use Atom.io editor
  2. Install the Atom package line-ending-converter
  3. Open the file in Atom > mark all > right mouse click > Convert Line Endings To > Unix Format
  4. Save file and upload to server (if JS file, open in browser to see if the linebreaks are correct now)

Some background information from here:

When downloading a file with Windows line-endings (CR+LF) in a text/ASCII mode, the server replaces LF with CR+LF, resulting in an incorrect CR+CR+LF. When opening such file in an Internal editor of WinSCP, the editor interprets the sequence as two line endings (CR and CR+LF) resulting in a blank line after each and every content line. When the file is saved, the internal editor saves two Windows line endings CR+LF and CR+LF. On upload they get converted to two LF’s. A workaround is to use an external editor and make sure WinSCP does not force text mode for edited files.

Upvotes: 1

cHao
cHao

Reputation: 86525

It's unlikely that FTP will "put everything on one line". More likely is that the file uses *nix-style line endings and you uploaded the file in binary mode to a Windows machine, or something like that.

See, different platforms have different sequences of characters to represent "new line". In Windows, that's most commonly CR+LF, but on *nix, it's just LF. Problem being, lots of software assumes stuff about what line ending a text file should have, and if it sees a LF character, it doesn't see it as a newline character. So everything looks like it's on one line. (I didn't think PHP was so anal about line endings, but i've seen other languages that freak out over it.)

Binary mode transmits the file byte for byte, and doesn't convert line endings. Which is great if you're down- or uploading an image or something. But if you're transferring text files in binary mode between machines running different OSes, you're likely to see the results of "incorrect" line endings.

The solution: Use ASCII mode for PHP files. Most FTP clients have settings for what mode to use, and even what mode to use for certain types of files. Since PHP files are text, they should probably be transferred in ASCII mode in most cases.

Upvotes: 3

Related Questions