Remove CRLF from windows PHP files.

On October 20, 2009, in How-to, Scripting, by Cubert aka (Cube Dweller)

I have had a few guys ask me, Shannon I have a Windows guy developing web aps in PHP but I run them on a Linux (LAMP) system. Some times the code seems to fail and I find the files populated with Carrage Return Line Feed or better seen while looking at a windows file on a linux system in a text editor ( ^M ). This has been know to have adverse effects on LAMP systems at times and could cause web errors.

Great we now know the why.. How do we fix it?

Simple, Linux provides the program to review a file and make the mods and we can the loop it through a directory to do all files. Here is the cmd you want to use.
find (path to top level folder) -type f -exec dos2unix {} \;

We use the program “find” to get and pipe our files to the cmd dos2unix which is the program that is actually doing the repairs. We use both programs together to produce the desired effect of removing all CRLF from a directory of windows files like a php website.

Remember always backup your files first, be safe.

Tagged with:

Leave a Reply


%d bloggers like this: