Code displayed in one line after FTP upload/download - php

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.

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.

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 ;)

Migrate web-pages from different char-sets to UTF-8

For the last years I used Notepad++ on Win XP SP2.
As I just have seen, the setting in Notepad++ is to encode new files in "ANSI" in "Windows Format". Basically all files on my harddisk should be ANSI files then, but I'm not sure.
Most .html-files have a charset-tag as "text/html; charset=iso-8859-1", but some have none.
Other files, especially text-files (for example keyword-lists) I stored with Firefox XPCOM-system, I don't know how they are currently encoded.
On Server-side I have Apache with PHP and MySql.
For Upload I used Filezilla.
Now the problem is: I want to use Japanes signs (or arabic, etc.). This only works partly.
I can get my selfmade Firefox-Application to constantly write or read UTF-8. But I can't check everytime which of the old files is which encoding.
Having just read Joel Spolsky's old article about UTF-8 strengthens my view that I simply have to get my whole system changed as much as possible to UTF-8.
As long as I have it running that way locally on my Hard-Disk I could just re-upload everything to the server.
So: How do I get all my files locally transfered to UTF-8?
And: Is it possible at all to have Win XP SP2 using constantly UTF-8 everywhere? Or do I have to check it with every program, or even worse with every file, that the right encoding is to be used.
How about files I get for example in E-Mails or via an USB-stick, or that I download in zip-files? (Or a thousand possibilities more.)
Update:
1.-4. went OK so far. I tried first with BOM, but without seems to be better.
So to 5.) Something I have to change there too. I changed as in 3.) the charset in the html-template-file, and the text coming from the template is displayed correctly. But the text coming from MySql/Php shows the UnknownChar-sign at some places currently, i.e. where there should be Umlaute äöü.
I have changed all collations for text fields in the MySql-Database via phpmyadmin to "utf8_unicode_ci", but that didn't do the trick.
Is it a php-issue, or do I only have to convert somehow the data in the MySql-Database once?
The beauty of UTF-8 is that it's a superset to ASCII, so if your html and php files only contain Latin alphabets (i.e. English and programing/HTML syntax), you don't need to convert the file at all. You can leave most of your file unchanged.
Should you find few exceptions that you want to convert it manually, you may open them up in Notepad++, and do 'Encoding' - 'Convert to UTF-8 (No BOM)'.
Yes, you do need to change/add <meta> charset tag to all the HTML files to make sure the browser render your files in UTF-8.
In Notepad++ you could set the new file to always open with 'UTF-8 (No BOM), Unix'. Also, check the tick on "Apply to ANSI files" so old file can be correctly saved to the new encoding. I suggest the format is because even though you are working on a Windows machine, the web servers usually runs Linux/BSD so the format is the native form (keeping files in native form is important especially when you are using a version control system).
Migrate a live site with database is a different issue. Data in MySQL comes with their own encoding, and from your question I cannot tell if you need to do it and how to do it. Need more specifics on that (if you need to).

Can I parse PHP locally without the use of a local host server?

Often, when I am developing in PHP, I want to see minor changes instantaneously. This requires me to either FTP to a web server and then refresh, or use a localhost server, both of which(as I understand) are essentially the same thing.
What I would like to know is, is there an IDE or other way to parse PHP output in a dynamic fashion? I guess what I am thinking of is the WYSIWYG pane in some editors able to display code real-time.
I understand the client-server paradigm, but would rather not have to upload the same file 40 times when making changes/error handling/etc.
You can always just save your files inside your localhost folder. Tools like xampp make running a php server effortless. Open the file in a browser, and whenever you make changes they will instantly be visible without having to copy the files around.
I've heard that ActiveState's Komodo is IDE for php behaving more-less the way you described (at least it has dynamic syntax check, so it has to parse the script locally).
Try Komodo Website.
You should setup your IDE to edit the ftp files in place. I use Edit Plus although I am sure others will do the same thing. In this mode, you open the files in the ftp directory browser (identical to the local one) and when you save, it is automatically uploaded to the server. You can also set it up to save the file locally in a backup folder since it sounds like you aren't using source control. You then set it up so when you CTRL+B, the remote file will open a browser inside the editor (Look under project settings for the root paths).
And yes, while the localhost webserver can be similar, there will be some incompatibilities you will have to overcome, especially if you are running windows and your host is a unix variant. For example: money_format() for one doesn't exist on windows, but is very usefull under linux. Also the configurations will be different even if the OS's are the same.
Good Luck!
This probably doesn't come up very often because it's so easy to install a local web server. What is your OS?
Also if your remote web server is accessible by SSH you could learn a screen oriented text-editor (such as the VI editor) and edit the source files on the server.
I feel the same way, but get around it easily by using an editor/IDE with FTP support. Configure your account, and then edit files locally, but the editor/IDE pushes them to the server when you save them. Notepad++ and PSPad both do this on Windows, and I use Gedit (default Gnome text editor) when on Fedora. A Google search for "FTP text editor" or "FTP IDE" might serve you well.
You could use the PHP command line, but that displays its output in plain text. You could take that output, save it as an HTML file, and display it with a browser, but I'd think that's more work than just setting up a local web server.
Aptana's PHP plugin has a built-in PHP server, so that's probably close enough to what you want, although I've never used it so I can't directly say anything about it.
I may be mistaken, but it seems like you want to see changes without having to restore/maximize browser and clicking 'refresh'. So in other words, you want to see results without the inconvenience of having to maximize browser, clicking refresh, the minimizing it to get back to the editor.
As far as i know, there is no IDE that will show PHP code dynamically in real time.
What you may want to do is, if you can, bind it so when you save a document, it automatically loads up a browser and refreshes the page for you.
Any IDE should notify you of syntax errors (normally with the wavy red underline), which will cut down on a lot of FTP-ing just to find you missed a semicolon.
Even though you say you don't want to use localhost, I would suggest using something like WampServer. The best thing about the latest version is you can install different PHP, MySQL and Apache "modules" and switch between them to mimic any server configuration. For example, "Apache 1.3, PHP 4.2" or "Apache 2, PHP 5.3" etc.

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

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.

Categories