ASCII vs Binary vs Auto?

49,245

Solution 1

The "Binary" transfer mode of FTP copies files exactly, byte for byte. Simple and straightforward.

When bringing text files between different operating systems, though, this might not be what you want -- different operating systems use different codes to represent line breaks. The "ASCII" mode exists for this purpose: it automatically translates all line endings from the source system's format to the destination's.

Not sure about "Auto", but I imagine it looks that the file's extension or something similar to decide whether it's a text file, and tries to guess the appropriate mode.

Which mode you want depends on exactly what you're doing with the files... if you're just copying them to back them up, then you'll probably want to copy in binary mode so they'll be exactly the same when you later restore them to the windows server again. If they need to be usable as text files (perhaps as config files for a cross-platform program?) on both sides, you'll want to use ASCII mode to translate them.

EDIT: As far as I can tell, FTPing files from Windows to Linux should never result in line breaks disappearing... however, if you copy them in ASCII mode, and then bring them back to the Windows server in binary mode, the Linux-style line endings might not be recognized on the Windows box. (Notepad won't see them; Wordpad will; YMMV with other software.)

(Today, such a convenience -- converting line endings automatically -- might seem odd in such a basic protocol as FTP. When FTP was invented, though, sending text files was the norm, and one of the goals of the protocol was to make this as easy as possible.)

Solution 2

There are differences indeed, and they will be messing with your non-plain-text file transfers unless you use the binary option - generally always use if ok :)

Solution 3

What operating system are you transferring files to/from? Linux/Unix use different line breaks then Windows. So if you generate a file on a linux computer and open it on windows it may look weird. There are utilities to correct this though in my experience if you use wordpad on windows instead of notepad you'll be fine.

Solution 4

Windows and Unix have different bytes at the end of lines (Windows a 0D 0A hex, Unix just a 0A). When transferring files in Binary mode, the bytes of a file will be moved from one computer unchanged. This is required for binary formats (e.g. ZIP files, Images, etc.) but can lead to problems with text files: E.g. notepad.exe on Windows will display a text file with just unix style line breaks as one long line, a unix editor might show ^M at the end of every line when seeing Windows style.

So technically for binary formats (zip, jpg, png, and endless others) you need to set the FTP to BINARY, while for text formats (HTML, PHP, CGI, etc.) you need to set it to ASCII.

Most good FTP programs also have an auto setting, which means they will determine the mode (BINARY or ASCII) depending on well known file extensions, e.g. it will automatically switch the transfer for JPG file to BINARY and send/receive a PHP file in ASCII mode.

Share:
49,245

Related videos on Youtube

thierry
Author by

thierry

My name is Jonathan Sampson; I'm a full-stack web developer located near Pensacola, FL, United States. Up until September of 2016 I served as a Program Manager on the Internet Explorer and Microsoft Edge team at Microsoft. Today I am fighting evil ad-tech at Brave Software. I'm a huge advocate of community, and open-source software. I had the great honor of being a community moderator on Stack Overflow from February 2010 to March 2013. Additionally, I try to give back when and where I can through GitHub. I recently did walkthroughs on the HTML, and CSS2.1 Specifications. These can be found online at http://youtube.com/jjdsampson. I'd like to kick off more regular hangouts discussing JavaScript, JavaScript frameworks, and modern CSS. I'm also available to help with open-web issues and cross-browser compatibility. Feel free to reach out to me on twitter, and I'll gladly assist to the best of my ability.

Updated on September 17, 2022

Comments

  • thierry
    thierry over 1 year

    I'm transferring files between servers and just started noticing that some of them are getting modified to be one long continuous line as opposed to having the returns and line-breaks they originally had. I'm assuming this has something to do with the transfer-type of my FTP Client which was originally set to "Auto," but sporting "Binary" and "ASCII" as additional options.

    In short, what are the differences between the ways I transfer a file from one server to another, and will these differences be capable of modifying the file in such a way as I mentioned above?

    Transfering FROM Windows TO Linux.

  • thierry
    thierry about 14 years
    I'm authoring these on windows in an IDE, and then moving them to the linux server. At a later date, I open them from the linux server to find the long continuous line.
  • Chopper3
    Chopper3 about 14 years
    I'm sure you could unpick them but it might take a long time, I'd be strongly tempted to retransmit them.
  • einstiien
    einstiien about 14 years
    Try running dos2unix and see if that corrects the lines
  • thierry
    thierry about 14 years
    Great description of both. I feel a lot less confused now.
  • Kiwy
    Kiwy about 10 years
    thank you I never understadnd the point of that ASCII mode, and now I will definitely only use binary mode