problems depending if we execute the php in windows or linux

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pepoy
Forum Newbie
Posts: 1
Joined: Wed Mar 16, 2005 12:37 pm

problems depending if we execute the php in windows or linux

Post by pepoy »

8O
Hi everybody,

I have a little problem with my following code:

Code: Select all

$path_completo_fichero = &quote;/tmp/file1.txt&quote;;
$CSVfile=&quote;/tmp/file2&quote;;
$id_company=2;

if (!($f1=fopen(&quote;$path_completo_fichero&quote;,&quote;r&quote;)))
trigger_error(&quote;I can't open the descriptor f1&quote;, E_USER_ERROR);
if (!($f2=fopen(&quote;$CSVfile&quote;,&quote;w+&quote;)))
trigger_error(&quote;I can't open the descriptor f2&quote;, E_USER_ERROR);
//Checking
if (($f1)&&($f2))
{
while (!feof($f1)) {
$buffer=fgets($f1,4096);
echo $buffer .&quote;\n&quote;;
$buffer=$id_company.&quote;, ,&quote;.$buffer;
echo $buffer .&quote;\n&quote;;
fwrite($f2,$buffer);
}
This code reads from the first file and writes in the second one adding the variable id_company and a white space. The problem we have is we execute this code in linux the code writes one more line than if we execute in windows. It seems a problem detecting the end of file:

Code: Select all

oscar,garayoa,pisano,navarro,914613838,645373838,oscar@pepe.com;
oscar1,garayoa,pisano,navarro,914613838,645373838,oscar@pepe.com;
oscar2,garayoa,pisano,navarro,914613838,645373838,oscar@pepe.com;

Code: Select all

2, ,oscar,garayoa,pisano,navarro,914613838,645373838,oscar@pepe.com;
2, ,oscar1,garayoa,pisano,navarro,914613838,645373838,oscar@pepe.com;
2, ,oscar2,garayoa,pisano,navarro,914613838,645373838,oscar@pepe.com;
2, ,
However, the last line '2, ,' doesn't appear in Windows.

Any help will be appreciated

Thanks
JL


feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

it probably has to do with the the two different operating systems use the end of line command.

I beileve linux is \n
while windows prefers \r

or something like that. Use what ever it will be deployed on. I use apache on my windows machine for development and find that the \n works just fine.
Post Reply