PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old May 13th, 2003, 05:45 PM
tikelele tikelele is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 3 tikelele User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
edit text files with php

I have the following example:
<?php
// Places the password into the mig.cnf file in the following format
// $protect['./mhtest']['mark'] = 'NLJpD6Gw6tYS2';

$cust_name = 'Harris';
$dir = 'mhtest';
$user = 'mark';
$pass = 'tikelele';
$salt = 'MG';
$passwd = crypt($pass, $salt);

$path = ("\$protect['./$dir']['$user']");

$fp = fopen("hello.txt", "a+");
fwrite($fp,"//$cust_name"."\n" .$path." = "."'" .$passwd."'".";" ."\n");

fclose($fp);
echo (done);


?>

I am trying to use it to append this text file:

<?php
blah blah.
blah blah.
blah blah.
?>

but it always appends to the bottom outside the closing php tag "?>" like this:

<?php
blah blah.
blah blah.
blah blah.
?>
//Harris
$protect['./mhtest']['mark'] = 'MGojrkbOSMreY';

I need it to append inside the tag like this:

<?php
blah blah.
blah blah.
blah blah.
//Harris
$protect['./mhtest']['mark'] = 'MGojrkbOSMreY';
?>

Who can help please?

Last edited by tikelele : May 13th, 2003 at 05:49 PM.

Reply With Quote
  #2  
Old May 13th, 2003, 09:26 PM
avit avit is offline
Not Yet Perfect
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: Squamish, BC
Posts: 111 avit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via ICQ to avit
PHP Code:
 $fp fopen("hello.txt""a+");
fseek($fp, -2);
fwrite($fp,"//$cust_name"."\n" .$path." = "."'" .$passwd."'".";" ."\n");
fwrite($fp"?>"); 
This assumes that your files end exactly with the 2 characters "?>" by seeking the pointer back 2 spaces, then writing your stuff and appending the closing tag again. If you do it this way, beware of empty spaces or lines at the end of your file. If you need it to be "smarter" then you'll have to read the file (or just the last X bytes of the file), then grep for the offset of the last "?>", and then place the file pointer there.

Reply With Quote
  #3  
Old May 14th, 2003, 08:21 AM
tikelele tikelele is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 3 tikelele User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I tried using the fseek flag that you sent--however it still appended to the end of the file.

Thank you for the input though.
BTW--tell me more about the other option you suggested.

Reply With Quote
  #4  
Old May 15th, 2003, 03:40 AM
avit avit is offline
Not Yet Perfect
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: Squamish, BC
Posts: 111 avit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via ICQ to avit
Sorry, I had to check the online documentation. Heh.
PHP Code:
 fseek($fp, -2SEEK_END); 

Regarding the other option, try something like this:
PHP Code:
// put your stuff away into a nice variable:
$stuff "//$cust_name"."\n" .$path." = "."'" .$passwd."'".";" ."\n";

// open your file:
$fp fopen("hello.txt""a+");

// seek 10 characters back from the end:
fseek($fp, -10SEEK_END);

// read 10 bytes:
$fp_tail fread($fp10);

// split those 10 bytes on every closing tag:
$tail_array split('\?>'$fp_tail);

// add your $stuff to the second-last chunk:
$tail_array[count($tail_array) -2] .= $stuff;

// concatenate the string back together with closing tags as glue:
$fp_tail implode('?>'$tail_array);

// seek back to where we want to write from (reading moved it):
fseek($fp, -10SEEK_END);

// write the new tail to the file:
fwrite($fp$fp_tail);

// close it:
fclose($fp); 

Reply With Quote
  #5  
Old May 15th, 2003, 06:56 AM
tikelele tikelele is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 3 tikelele User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
i was not able to get either solution to work--it still appended to the end and I got the following error message:

Warning: REG_BADRPT:rrepetition-operator operand invalid in d:\"i am replacing the path"... on line 42

also addind SEEK_END appended at the end.

any suggestions

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > edit text files with php


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway