Current location: Hot Scripts Forums » Programming Languages » PHP » Writing to .txt file in php


Writing to .txt file in php

Reply
  #1 (permalink)  
Old 06-16-05, 01:31 PM
897236489 897236489 is offline
Newbie Coder
 
Join Date: Apr 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Writing to .txt file in php

I created a chat script which uses a flat file text database, the problem is when the chat logs are saved into the txt file the information is added to the top of the file and i would likeit to be added to the bottom.

How can this be done?

This is the code, what do i need to change?

PHP Code:

$user $username ;

$f file($file) ;
$users = array($user) ;
header ("Content-Type: text/html; charset=utf-8") ; 
$fp fopen($file"w") ;
if (!
$fp) { die ('Can not write to file .') ; }
for (
$i=0; isset($f[$i])||$i==0$i++) 
{
    
$e=explode('||'$f[$i]) ;
    if (
$e[2]=="\r\n" && !in_array($e[0], $users) && ( time() - $e[1] < $refresh*) ) $users[]=$e[0] ;
    if (
$i==0) { fputs($fp$user."||".time()."||\r\n") ;
        if ( isset(
$HTTP_POST_VARS['msg']) ) fputs ($fp$user."||".time()."||".str_replace("||"""str_replace("\n"""htmlspecialchars(stripslashes($HTTP_POST_VARS['msg']))))."\r\n") ;    }
    if ( (
$e[0]!=$user && $e[2]=="\r\n") || ( $i<$max && ($e[1]+$remain)>time() && $e[2]!="\r\n") ) fputs($fp$f[$i]) ;
}
fclose($fp) ;

if (isset(
$HTTP_GET_VARS['f1']))

    
$f2 file($file) ; 
__________________
www.fourthousandholes.com
Reply With Quote
  #2 (permalink)  
Old 06-16-05, 02:34 PM
wheezy360's Avatar
wheezy360 wheezy360 is offline
Newbie Coder
 
Join Date: Nov 2003
Location: Toronto, ON
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
If you change the fopen() mode from w to a+, this should place the file pointer at the end of the file for appending.

Change:
PHP Code:

$fp fopen($file"w"); 

To:
PHP Code:

$fp fopen($file"a+"); 

Reply With Quote
  #3 (permalink)  
Old 06-18-05, 06:42 AM
sarat.pediredla sarat.pediredla is offline
Newbie Coder
 
Join Date: Jun 2005
Location: Newcastle upon Tyne UK
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Or if you want to run around

This code will allow you to append anywhere in the file.

PHP Code:

 $string "Message to write to log";

       
$filehandle fopen ("/home/name/sqllogs/phpsqlerr.txt"'r+');
   
fseek $filehandle,0SEEK_END);
   
fwrite $filehandle$string."\n" );
   
fclose ($filehandle); 
__________________

--------------------------------
Sarat Pediredla
[Writing a lot about nothing]
Blog : Space & Beyond


<a href="http://www.spreadfirefox.com/?q=affiliates&amp;id=108865&amp;t=68"><img border="0" alt="Get Firefox!" title="Get Firefox!" src="http://sfx-images.mozilla.org/affiliates/Buttons/88x31/take.gif"/></a>
Reply With Quote
  #4 (permalink)  
Old 06-20-05, 01:25 AM
klaniak klaniak is offline
Wannabe Coder
 
Join Date: Apr 2005
Location: Underground
Posts: 119
Thanks: 0
Thanked 0 Times in 0 Posts
You can also try:
$fp = fopen($file, "a");
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP write to text file veeco Script Requests 2 02-10-10 02:48 PM
Saving php generated file as html file GS300 PHP 0 12-29-04 03:34 AM
move files around an ftp server, with php file upload script? wapchimp PHP 2 12-19-04 07:27 AM
Writing from form to php config file Squid44th PHP 1 04-22-04 02:16 PM
php write to file tiny Script Requests 2 01-14-04 09:12 PM


All times are GMT -5. The time now is 06:50 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.