Php adds non-ascii symbols to files

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
fa
Forum Newbie
Posts: 1
Joined: Thu Nov 02, 2006 3:53 am

Php adds non-ascii symbols to files

Post by fa »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello, everyone.
I have s php script that reads a contents of a file to TEXTAREA html element. After I post this contents back to the server via form, and the same script writes it back to the file. The problem is that php adds some non-ascii characters to a file.
Please, help me resolve this.
Here is the script:

Code: Select all

<?
  $file="file.txt";
  if ($_POST['save']=='1') {
    $handle=fopen($file,'w');
    $data=fwrite($handle,$_POST['data']);
    fclose($handle);
  }
  $handle=fopen($file,'r');
  $data=fread($handle,filesize($file));
  fclose($handle);
?>
  <html><body><form action=<? echo $_SERVER['PHP_SELF']; ?> method=POST><textarea name=data><? echo $data; ?></textarea><input type=hidden name=save value=1><br><input type=s
ubmit value=Ok></form></body></html>

When I do diff to a file before and after modification by this script, it returns the following:

Code: Select all

user@laptop# diff file.txt file2.txt
1,4c1,4
< 1234
< qwer
< asdf
< zxcv
---
> 1234
> qwer
> asdf
> zxcv

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I would suspect it's actually a conflict between your browser's native carriage return and the server's carriage return.
Post Reply