Jump to content

[SOLVED] ftp_put - Adds empty lines between every line on upload.


Recommended Posts

Hi.

 

I have a wierd problem i hope you can help me with.

I have a CMS system that uses php's ftp functions to keep the file ownership to the user, not apache.

So when a file is uploaded, it is uploaded to a temp directory, and i use "ftp_put" to move it to the right position, with correct ownership. However, i installed the system on a server today, and when i upload php files using this method, it adds empty lines between every line. this does not happen to other non-binary files like javascript files.

I have used this method for a long time, and i have never seen this before.

 

Some info:

Transfer mode is FTP_ASCII

Server is running suPHP (Is this the problem?)

 

Example:

(before transfer)

<?php
$line   = "just an example";
$line2 = "just an example too";
echo "This is ".$line;
echo "This is ".$line2;
?>

(after transfer)

<?php

$line   = "just an example";

$line2 = "just an example too";

echo "This is ".$line;

echo "This is ".$line2;

?>

 

This might not seem like a "problem", but i do have som code that cant have empty lines.

whats your code for uploading the file?

 

also why does it need to be ran on suPHP? if you log into the FTP server as the user then it will be under their permissions anyway..?

 

Liam

 

suPHP was not my choice, but the server i installed it on was running it, and i cant keep rewriting the cms everytime a customer has a host with suPHP. The code for uploading is just a normal upload to a temp dir, and then i use ftp_put to move it to the desired location from the temp dir. and then i delete the file from the temp dir when finished. Nothing special  to it..

 

try switching to FTP_BINARY..

 

also on some FTP server theirs an option to resolve this, its a common problem when a file is written on a Macintosh then opened on a PC,

Why FTP_BINARY? a php script is not binary, and should be transferred as ASCII. I use Linux, so the file is written on linux. the server is also running linux. I have opened the file in the same program the file is written on.

 

 

Why FTP_BINARY? a php script is not binary, and should be transferred as ASCII. I use Linux, so the file is written on linux. the server is also running linux. I have opened the file in the same program the file is written on.

There is no harm done in transferring files as binary and it's the only way to ensure that the files remain intact. The ASCII transfer mode allows the software to "translate" the files to fit for the recieving operating system, i.e. modify the line endings to what the operating system uses. This causes some rather weird behavior sometimes (for example CRLF translates to CRCRLF on Linux -> Windows)

 

If you want to find more information, I would recommend checking what kind of line endings the file originally has (such as if you editor is using something nonstandard for the operating system) and what kind of line endings the file has after the extra lines are there.

 

Using binary transfer is, however, much easier option, as it will ensure there is no magical behavior.

its true script should be sent via ASCII, and uploading via BINARY can cause problems (ie error 500) but its worth a shot, i have used it a few time.. infact i have had to upload a zip and use the server tools to unzip the files to get them to work on a server.

 

thats why i said "Try" switching to binary

Okay.. I didn't know that. I will try the binary transfer and see how that works. Hopefully that will do the trick.

Btw. I use Kate as editor. dont think there is anything special about is as it is for editing source files like php.

try switching to FTP_BINARY..

 

also on some FTP server theirs an option to resolve this, its a common problem when a file is written on a Macintosh then opened on a PC,

 

I switched to binary and you are right, the file is not touched. Any idea how this can happen? The server is running ProFTPD 1.3.0, which is the same as my own server, and my server does not touch the files in ASCII mode.

I tried passive mode too, but the problem is still there. I have tried all i can think of, and binary mode seems to be the only way to keep the files intact. But i had some problems some time ago with binary mode on ascii files, that is why i really want to find the problem.. The strange thing is if i have a *.js file written on the same editor as the php files, the js file is not changed, only php files, and the only difference i know in the servers, is that the server with the problem is running suPHP.

 

Thanks so far

I discovered something strange. Files written in windows is not changed, only the files written on a linux enviroment. So it must have something to do with the line endings. In the settings of my editor, line endings are set to UNIX.

 

With FTP_BINARY, i now get a 500 internal server errror sometimes.

Problem solved. This is a reply i got from the host (Translated to english):

 

According to hexdump, the file uses wierd line shifts:

 

0000000 3f3c 0a0d 2a2f 2d2d 2d2d 2d2d 2d2d 2d2d

0000010 2d2d 2d2d 2d2d 2d2d 2d2d 2d2d 2d2d 2d2d

 

The first line, including lineshift is made of 4 octals:

0x3F, 0x3C, 0x0A, 0x0D. This represents the ASCII-values "<", "?", LF, CR (LF = line feed, CR = carriage return). This means that the lineshift is made of octals LF, CR.

 

Standard lineshift:

- Unix: LF (line feed => 0x0A => 10)

- Windows/DOS: CR, LF (carriage return, line feed => 0x0D, 0x0A => 13, 10

 

Så the problem is that KATE (The editor i use), does something to my files, even when lineshift is set to Windows/DOS.

I guess i have to ask in another forum how to solve that problem. (Altough any advice here is very welcome :))

 

This command fixes the files, but when i open the files in kate, and save. the file is "corrupted" again :(

 

 cat before.php | sed 's/^M//g' > after.php 

(^M is made of CTRL+V + CTRL+M)

 

Thanks everyone.

With FTP_BINARY, i now get a 500 internal server errror sometimes.

i was worried about that..

 

windows machines add /r/n the end of the line, while *nix only adds /n (also a Macintosh only adds /r)

 

try saving as text-only

 

Actually, i solved it right after i posted. I removed the "Remove spaces after line endings" and its now saving the files correctly. All that is left now is to write a function that recursivly converts all my files (and thats a lot of files) ;D

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.