How to remove excess whitespace added to code by FTP program? - php

This happens repeatedly and is very annoying. I upload some PHP code to a client's server. A few weeks pass. They ask for a change to be made and I re-download the code as they've made some changes. However, my code which used to be neat and tidy the last time I looked at it now has an extra lines of whitespace added everywhere. So now where I had two lines of space between some code, it now has 3. Where I had a bunch of lines sticking together because they were part of the same for loop or such, they're all scattered around now and there's no way to distinguish them.
Is there any program/utility to fix this?

Upload in binary mode instead of ascii. Ascii mode is changing all your linefeeds (unix end of line character) into carriage returns + linefeeds (Windows end of line characters).

You may also be having a problem with the other editor using tabs when you are using spaces (you are using spaces, right?). I have seen similar problems when sharing source between developers on Linux/OSX and Windows.

I'll bet this is caused by systems trying to convert text files created in a Windows System to files being used by a Unix/Linux system (and back again).
Windows uses both a carriage return and a line feed and I think Unix uses only a line feed (or was it a carriage return).
I used Ultra Edit as my main text editor (not that Emacs and vi don't rule :o) and it has a DOS mode and a Unix mode for just this kind of thing.

Force your client to transfer all files in binary mode.
This is also useful when you have unicode text in your files, you never know what assumptions text-mode might make!

At a guess, I would say that you are developing on a Unix / Max system, but the customer is running on a Windows system (or vice-versa). I would also guess that you are uploading / downloading your files in Binary mode.
The windows editor is probably converting the Unix LF to a LF/CR pair, which your editor thren treats as two new lines.
If you upload and download in ASCII mode, the files will be automatically converted between the two formats.

Related

PHP file losing formatting after FTP upload

I am using WinSCP to transfer files to an FTP site. I have a situation currently where one specific file within a folder loses all of its formatting when it is uploaded causing the PHP file to no longer work.
All other PHP files within the folder work correctly when uploaded.
I can't understand why just one file could be affected in this way. Can anyone shed any light on the situation?
The file was probably transferd via ASCII mode which will modify the encoding and the line endings of the file.
As you have not stated what exactly do you mean by "losing formatting", it's difficult to answer, anyway:
As per src's answer, if the line endings change due to ASCII/text mode transfers, the resulting converted file can be perceived as if it lost formatting, if opened in an editor that does not support the target line endings. Though that hardly explains why there's only one affected file. Although can WinSCP technically choose a different transfer mode for example based on a file size or modification timestamp, if configured so, I doubt you did. Also note that WinSCP defaults to binary transfer mode. It would help if you state what transfer mode do you actually use with WinSCP. Definitive source for this information is WinSCP session log file. Also sharing relevant part of a log file would also help with investigation.
Another possibility is that the affected source file was created with a different line endings in the first place (like in a different editor than you use usually). As such the problem would have nothing to do with transfer mode, or WinSCP. And the difference is possibly revealed only after you open the files using a third editor on the remote side that supports only one of the line ending formats.
Though in both of these cases, the file should still work in PHP, as PHP supports both Unix and Windows line endings. Possibly the source file has such a strange format that during ASCII/text mode transfer, the server got confused and converted the file incorrectly. But that's just a wild guess.
Again, we need more information to help you.

NEWLINE in windows going to UNIX

I am developing on PHPandAPACHE on windows xampp environment.
I have some files I read and write to.
I do use definitions of NEWLINES (\r\n for Windows, \n for UNIX), but isn't it that when I port to UNIX I am going to have to convert all these files to UNIX text formats?
Is this not a door to lots of weird annoying bugs ?
Thanks
Yes, this can be any annoying problem. You will need address it somehow. I wrote my own C++ utility using fstream/sstream, but there are lots of solutions out there. There's actually a stack overflow which mentions a few ways to do this:
How to convert DOS/Windows newline (CRLF) to Unix newline (\n) in a Bash script?

Notepad++ put all my PHP into one line

I edited a bunch of PHP files (with N++) on Computer A, uploaded it to a server, downloaded it to computer B (with N++), and they lost all their line breaks. I've done this routine many times and never had this problem.
I've tried
EOL Conversion
Opening in different text editors
Search/replaces
Everything I found on Google/SO
Open up the file in Notepad++, choose Edit -> EOL Conversion -> Windows. I kept having this problem and somehow it had been set to Mac. Transferring in binary mode worked, but it was a hack. This is better.
There seems to be a question missing?
If you want to format it, you might just want to let a code-editor that knows indentation (auto-formatting) fix it for you. Eclipse can, I'm sure netbeans can too.
I think eclipse auto-formats everything you paste automagically, but otherwise just find the formatting commands ;)

Transferring a site - various file issues

I am putting an old website up on a new server and i've noticed various issues. Firstly, if i look at any of the files locally they all have double line spacing - anywhere there is a newline there has also been a blank line inserted. This is the same in netbeans and notepad++. Then, when i SFTP the files to the server and view them in Vi i see ^M at the end of each line. The final thing is that when i'm having issues making the site actually work, i've tracked this down to a common includes.php file which when i view it has been converted to a single line of code - i can still see ^M but there isnt a single newline in there. at the bottom of vi it actually says: "includes.php" [noeol]
I'm working locally on windows, i do have a Mac aswell i could try things on. My remote server is running Ubuntu 9
Please let me know if this question would be better on serverfault
Run the files through any of a number of standard utilities and save yourself a lot of time. I think dos2unix is usually on most distributions. If not it should be in a repository somewhere.
(Yes, this probably a serverfault question IMO.)
Snaken, had the same exact thing happen to me last night. I think I chalked it up to Netbeans, because it's never manifested itself in Notepad++ in several years of use. Good news is that if you open in notepad++ and re-do the returns, the tabs will reset themselves without fail. The bad news is that this is manual. For me, it was only three files, so no biggie.
This explains how to remove the ^m from line end: http://www.tech-recipes.com/rx/150/remove-m-characters-at-end-of-lines-in-vi/

Code displayed in one line after FTP upload/download

When I upload/download some HTML/CSS/… file to FTP server, sometimes something puts every line of code in one line making it completeply unreadable. That something happens every now and then and I’m still looking for an explanation for this behaviour. What could cause this?
It has to do with text file line endings and FTP transfer modes.
Text files in Windows use a combination of carriage return and line feed at the end.
Text files in Mac OS9 and down use carriage return only.
Text files in UNIX and its clones (including OSX) use line feed only.
It sounds like you're pulling a UNIX-style text file to a Windows system in binary mode and then trying to view it in an editor that doesn't understand these differences.
FTP clients have an ASCII (or ASC) transfer mode to do these conversions for you. It's not usually turned on by default, though, as it messes up binary files.
Two solutions are to use an editor that understands the differences or use an FTP client that allows you to tell it that specific file extensions should be transferred in ASC mode.
P.S. This is probably better asked on superuser or serverfault.
This problem arises when we are working with Notepad++, Notepad editors, main importantly hosting servers. I had an same issue and fixed it by opening file again through Dreamweaver and set the content properly and uploaded to server Again. Works fine.

Categories