Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-29-2007, 09:38 PM   PM User | #1
srule_
Regular Coder

 
Join Date: Jul 2007
Posts: 571
Thanks: 25
Thanked 28 Times in 28 Posts
srule_ is an unknown quantity at this point
fwrite() - Content on New Lines

Hello,
I am following a tutorial teaching me about the multiple read/right operations.

the code below works fine in that when i submit text into the form my textfile will update. the problem is that the updat is not on a new line, it is simply shown beside the old text.

the tutorial said to add "\n" (as i did in my code below) however this is not working form me

PHP Code:
// open the file in append mode
  
$file fopen($_SERVER['DOCUMENT_ROOT'].'/update/filetest05.txt''a');
  
// write the contents after inserting new line
  
fwrite($file"\n$contents");
  
// close the file
  
fclose($file);
  } 
any help would be great.
srule_ is offline   Reply With Quote
Old 07-29-2007, 10:01 PM   PM User | #2
matak
Banned

 
Join Date: Apr 2007
Posts: 428
Thanks: 29
Thanked 5 Times in 5 Posts
matak is on a distinguished road
On windows XP this works fine. Maybe you are using Mac, or Linux, i know that there could be different way to go to new line on those OS's.

As for something else too you could check

A list of possible modes
for fopen(), that is that 'a' you are using in fopen. And you could maybe use other more convenient way.

In your function instead of first setting \n$content, i would set $content\n beacouse 'a' mode

Quote:
Originally Posted by php.net
Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
So if it's first time your writing to file it will create empty line on the begining of the file, and new lines at the end of the $content, so you will have empty line at the begining of the file...
matak is offline   Reply With Quote
Old 07-30-2007, 03:31 AM   PM User | #3
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,910
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
it depends on which application you use to view the file, notepad for example requires "\r\n" but many editors autodetect the newline type and will display the newline regardless.

e.g. on windows I find I have to use "\r\n" if I want say notepad to display the newline whilst all linux editors seem happy with "\n".
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is online now   Reply With Quote
Old 07-30-2007, 05:23 AM   PM User | #4
srule_
Regular Coder

 
Join Date: Jul 2007
Posts: 571
Thanks: 25
Thanked 28 Times in 28 Posts
srule_ is an unknown quantity at this point
im viewing the output on a linux server so the book said to only use "\n". It also said to put the \n infront.
regradless i had already tryed all possible variotions and none worked.
srule_ is offline   Reply With Quote
Old 07-30-2007, 05:28 AM   PM User | #5
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,293
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
Wait, you are saying after you echo the new text file its not correctly formatted or are you saying its not correctly formatted when viewing the text file?

If you are saying that after echoing its not correctly formatted then this normal. New lines mean nothing in HTML, you need to use the nl2br function to maintain the new line characters.
__________________
||||If you are getting paid to do a job, don't ask for help on it!||||
_Aerospace_Eng_ is offline   Reply With Quote
Old 07-30-2007, 06:17 AM   PM User | #6
matak
Banned

 
Join Date: Apr 2007
Posts: 428
Thanks: 29
Thanked 5 Times in 5 Posts
matak is on a distinguished road
Quote:
Originally Posted by _Aerospace_Eng_ View Post
New lines mean nothing in HTML, you need to use the nl2br function to maintain the new line characters.
YES! If you are viewing it in a browser, you will not see new lines there like aerospace said.

Open your text file in some editor, or do a view source! If there is nothing in your text file, than probably your path is invalid.
matak is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:00 AM.


Advertisement
Log in to turn off these ads.