RE: [PHP] Modifying Existing Text File From PHP Is Not Working

2010-06-27 Thread Ashley Sheridan
On Sun, 2010-06-27 at 12:11 -0400, Alice Wei wrote:

 
  Date: Sun, 27 Jun 2010 12:01:50 -0400
  To: aj...@alumni.iu.edu; php-general@lists.php.net
  From: tedd.sperl...@gmail.com
  Subject: Re: [PHP] Modifying Existing Text File From PHP Is Not Working
  
  At 7:20 AM -0400 6/27/10, Alice Wei wrote:
  Hi,
  
  I have the code as in the following:
  
  ?php
  
  $name= Test;
  $phone= 123-456-7890;
  $email = myem...@mymail.com;
  $comments = Test Only;
  
  $string = message\nname . $name . /name\nphone . $phone 
  . /phone\nemail.
 $email . /email\ncomments .  $comments . 
  /comments\n/message;
  
  //If file exists. append, otherwise create
  $file = messages.xml;
  $fh = fopen($file,a);
  $lines = file($file);
  
  //Output a line of the file until the end is reached
  foreach($lines as $line) {
  
  if (trim($line) == messages) {
  
   $line = messages . $string; // If string, change it.
   echo $line . br;
   fwrite($fh, $line);
 
  }
  else {
 
   // If not don't write anything
  echo $line . br;
  
  }
 fclose($file); // Close the file.
 }
  ?
  
  For some reason, it is writing out the lines, but it is not 
  f-writing to the file. Here is the file that is meant to be edited:
  
  messages
  message
  nameMe/name
  phone123-456-7890/phone
  emailt...@test.com/email
  commentsThis is my message board!/comments
  /message
  /messages
  
  Have I missed something here? How come the file cannot perform fwrite 
  actions?
  
  Thanks for your help.
  
  Do you have permission to write to the file?
  
  Check the permissions of the file.
  
  Cheers,
  
  tedd
  
 
 I believe I do, since I set my folder permission of this script with modify, 
 read, execute and delete. I don't see any errors regarding this either. I 
 have changed my code to having the curly brace before I close the file 
 handler, if that helps any. Yet, it is still not modifying the file. Have I 
 missed something else here?
 
 Thanks for your help.
 
 Alice
  -- 
  ---
  http://sperling.com  http://ancientstones.com  http://earthstones.com
 
 _
 The New Busy is not the old busy. Search, chat and e-mail from your inbox.
 http://www.windowslive.com/campaign/thenewbusy?ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_3


It might be some weird Windows thing. I know that some of the more
recent versions of Windows, in an attempt to be more secure, make you
jump through hoops to write to a file, even when the permissions are set
correctly. Unfortunately, I don't have access to a system with IIS so
there's no way I can test your code on that.

Thanks,
Ash
http://www.ashleysheridan.co.uk




RE: [PHP] Modifying Existing Text File From PHP Is Not Working

2010-06-27 Thread Alice Wei


Subject: RE: [PHP] Modifying Existing Text File From PHP Is Not Working
From: a...@ashleysheridan.co.uk
To: aj...@alumni.iu.edu
CC: tedd.sperl...@gmail.com; php-general@lists.php.net
Date: Sun, 27 Jun 2010 17:16:04 +0100






  
  


On Sun, 2010-06-27 at 12:11 -0400, Alice Wei wrote:


 Date: Sun, 27 Jun 2010 12:01:50 -0400
 To: aj...@alumni.iu.edu; php-general@lists.php.net
 From: tedd.sperl...@gmail.com
 Subject: Re: [PHP] Modifying Existing Text File From PHP Is Not Working
 
 At 7:20 AM -0400 6/27/10, Alice Wei wrote:
 Hi,
 
 I have the code as in the following:
 
 ?php
 
 $name= Test;
 $phone= 123-456-7890;
 $email = myem...@mymail.com;
 $comments = Test Only;
 
 $string = message\nname . $name . /name\nphone . $phone 
 . /phone\nemail.
$email . /email\ncomments .  $comments . 
 /comments\n/message;
 
 //If file exists. append, otherwise create
 $file = messages.xml;
 $fh = fopen($file,a);
 $lines = file($file);
 
 //Output a line of the file until the end is reached
 foreach($lines as $line) {
 
 if (trim($line) == messages) {
 
  $line = messages . $string; // If string, change it.
  echo $line . br;
  fwrite($fh, $line);

 }
 else {

  // If not don't write anything
 echo $line . br;
 
 }
fclose($file); // Close the file.
}
 ?
 
 For some reason, it is writing out the lines, but it is not 
 f-writing to the file. Here is the file that is meant to be edited:
 
 messages
 message
 nameMe/name
 phone123-456-7890/phone
 emailt...@test.com/email
 commentsThis is my message board!/comments
 /message
 /messages
 
 Have I missed something here? How come the file cannot perform fwrite 
 actions?
 
 Thanks for your help.
 
 Do you have permission to write to the file?
 
 Check the permissions of the file.
 
 Cheers,
 
 tedd
 

I believe I do, since I set my folder permission of this script with modify, 
read, execute and delete. I don't see any errors regarding this either. I have 
changed my code to having the curly brace before I close the file handler, if 
that helps any. Yet, it is still not modifying the file. Have I missed 
something else here?

Thanks for your help.

Alice
 -- 
 ---
 http://sperling.com  http://ancientstones.com  http://earthstones.com
  
_
The New Busy is not the old busy. Search, chat and e-mail from your inbox.
http://www.windowslive.com/campaign/thenewbusy?ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_3




It might be some weird Windows thing. I know that some of the more recent 
versions of Windows, in an attempt to be more secure, make you jump through 
hoops to write to a file, even when the permissions are set correctly. 
Unfortunately, I don't have access to a system with IIS so there's no way I can 
test your code on that.


Well, my problem is not entirely solved, but I found that when I changed the 
file name extension from xml to txt, it is now letting me edit the file now. 
Only, the file is not writing in the way it is supposed to be. I used a+ 
originally, and it is writing outside my messages/messages root element. 

I tried w, and it wipes the file out. So, this proves that it is now modifying 
and saving it correctly. My question is, what kind of parameter should I use to 
add the things I would like to add after the messages without editing 
anything else in the file. 

Thanks for your help.

Alice







Thanks,

Ash

http://www.ashleysheridan.co.uk





  
_
Hotmail is redefining busy with tools for the New Busy. Get more from your 
inbox.
http://www.windowslive.com/campaign/thenewbusy?ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_2

RE: [PHP] Modifying Existing Text File From PHP Is Not Working

2010-06-27 Thread Ashley Sheridan
On Sun, 2010-06-27 at 12:23 -0400, Alice Wei wrote:

 
 Subject: RE: [PHP] Modifying Existing Text File From PHP Is Not Working
 From: a...@ashleysheridan.co.uk
 To: aj...@alumni.iu.edu
 CC: tedd.sperl...@gmail.com; php-general@lists.php.net
 Date: Sun, 27 Jun 2010 17:16:04 +0100
 
 
 
 
 
 
   
   
 
 
 On Sun, 2010-06-27 at 12:11 -0400, Alice Wei wrote:
 
 
  Date: Sun, 27 Jun 2010 12:01:50 -0400
  To: aj...@alumni.iu.edu; php-general@lists.php.net
  From: tedd.sperl...@gmail.com
  Subject: Re: [PHP] Modifying Existing Text File From PHP Is Not Working
  
  At 7:20 AM -0400 6/27/10, Alice Wei wrote:
  Hi,
  
  I have the code as in the following:
  
  ?php
  
  $name= Test;
  $phone= 123-456-7890;
  $email = myem...@mymail.com;
  $comments = Test Only;
  
  $string = message\nname . $name . /name\nphone . $phone 
  . /phone\nemail.
 $email . /email\ncomments .  $comments . 
  /comments\n/message;
  
  //If file exists. append, otherwise create
  $file = messages.xml;
  $fh = fopen($file,a);
  $lines = file($file);
  
  //Output a line of the file until the end is reached
  foreach($lines as $line) {
  
  if (trim($line) == messages) {
  
   $line = messages . $string; // If string, change it.
   echo $line . br;
   fwrite($fh, $line);
 
  }
  else {
 
   // If not don't write anything
  echo $line . br;
  
  }
 fclose($file); // Close the file.
 }
  ?
  
  For some reason, it is writing out the lines, but it is not 
  f-writing to the file. Here is the file that is meant to be edited:
  
  messages
  message
  nameMe/name
  phone123-456-7890/phone
  emailt...@test.com/email
  commentsThis is my message board!/comments
  /message
  /messages
  
  Have I missed something here? How come the file cannot perform fwrite 
  actions?
  
  Thanks for your help.
  
  Do you have permission to write to the file?
  
  Check the permissions of the file.
  
  Cheers,
  
  tedd
  
 
 I believe I do, since I set my folder permission of this script with modify, 
 read, execute and delete. I don't see any errors regarding this either. I 
 have changed my code to having the curly brace before I close the file 
 handler, if that helps any. Yet, it is still not modifying the file. Have I 
 missed something else here?
 
 Thanks for your help.
 
 Alice
  -- 
  ---
  http://sperling.com  http://ancientstones.com  http://earthstones.com
 
 _
 The New Busy is not the old busy. Search, chat and e-mail from your inbox.
 http://www.windowslive.com/campaign/thenewbusy?ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_3
 
 
 
 
 It might be some weird Windows thing. I know that some of the more recent 
 versions of Windows, in an attempt to be more secure, make you jump through 
 hoops to write to a file, even when the permissions are set correctly. 
 Unfortunately, I don't have access to a system with IIS so there's no way I 
 can test your code on that.
 
 
 Well, my problem is not entirely solved, but I found that when I changed the 
 file name extension from xml to txt, it is now letting me edit the file now. 
 Only, the file is not writing in the way it is supposed to be. I used a+ 
 originally, and it is writing outside my messages/messages root element. 
 
 I tried w, and it wipes the file out. So, this proves that it is now 
 modifying and saving it correctly. My question is, what kind of parameter 
 should I use to add the things I would like to add after the messages 
 without editing anything else in the file. 
 
 Thanks for your help.
 
 Alice
 
 
 
 
 
 
 
 Thanks,
 
 Ash
 
 http://www.ashleysheridan.co.uk
 
 
 
 
 
 
 _
 Hotmail is redefining busy with tools for the New Busy. Get more from your 
 inbox.
 http://www.windowslive.com/campaign/thenewbusy?ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_2


There isn't any switch that suddenly recognises that you want to add
nodes inside of an XML document, you have to write the code for that
yourself.

There are two main ways to go about this:


 1. Read in the entire file with file(), create a new array with
your new content to insert, and splice() it back into the main
array you read in at the start, then write the whole of that to
the original file, overwriting anything in it.
 2. Use XML functions to read in the contents of the file, add in
the new nodes as you need, and overwrite the whole file again
with the new DOM output


Either way, you're going to have to overwrite the existing file each
time.

Thanks,
Ash
http://www.ashleysheridan.co.uk




RE: [PHP] Modifying Existing Text File From PHP Is Not Working

2010-06-27 Thread Alice Wei

Subject: RE: [PHP] Modifying Existing Text File From PHP Is Not Working
From: a...@ashleysheridan.co.uk
To: aj...@alumni.iu.edu
CC: tedd.sperl...@gmail.com; php-general@lists.php.net
Date: Sun, 27 Jun 2010 17:29:05 +0100






  
  


On Sun, 2010-06-27 at 12:23 -0400, Alice Wei wrote:


Subject: RE: [PHP] Modifying Existing Text File From PHP Is Not Working
From: a...@ashleysheridan.co.uk
To: aj...@alumni.iu.edu
CC: tedd.sperl...@gmail.com; php-general@lists.php.net
Date: Sun, 27 Jun 2010 17:16:04 +0100






  
  


On Sun, 2010-06-27 at 12:11 -0400, Alice Wei wrote:


 Date: Sun, 27 Jun 2010 12:01:50 -0400
 To: aj...@alumni.iu.edu; php-general@lists.php.net
 From: tedd.sperl...@gmail.com
 Subject: Re: [PHP] Modifying Existing Text File From PHP Is Not Working
 
 At 7:20 AM -0400 6/27/10, Alice Wei wrote:
 Hi,
 
 I have the code as in the following:
 
 ?php
 
 $name= Test;
 $phone= 123-456-7890;
 $email = myem...@mymail.com;
 $comments = Test Only;
 
 $string = message\nname . $name . /name\nphone . $phone 
 . /phone\nemail.
$email . /email\ncomments .  $comments . 
 /comments\n/message;
 
 //If file exists. append, otherwise create
 $file = messages.xml;
 $fh = fopen($file,a);
 $lines = file($file);
 
 //Output a line of the file until the end is reached
 foreach($lines as $line) {
 
 if (trim($line) == messages) {
 
  $line = messages . $string; // If string, change it.
  echo $line . br;
  fwrite($fh, $line);

 }
 else {

  // If not don't write anything
 echo $line . br;
 
 }
fclose($file); // Close the file.
}
 ?
 
 For some reason, it is writing out the lines, but it is not 
 f-writing to the file. Here is the file that is meant to be edited:
 
 messages
 message
 nameMe/name
 phone123-456-7890/phone
 emailt...@test.com/email
 commentsThis is my message board!/comments
 /message
 /messages
 
 Have I missed something here? How come the file cannot perform fwrite 
 actions?
 
 Thanks for your help.
 
 Do you have permission to write to the file?
 
 Check the permissions of the file.
 
 Cheers,
 
 tedd
 

I believe I do, since I set my folder permission of this script with modify, 
read, execute and delete. I don't see any errors regarding this either. I have 
changed my code to having the curly brace before I close the file handler, if 
that helps any. Yet, it is still not modifying the file. Have I missed 
something else here?

Thanks for your help.

Alice
 -- 
 ---
 http://sperling.com  http://ancientstones.com  http://earthstones.com
  
_
The New Busy is not the old busy. Search, chat and e-mail from your inbox.
http://www.windowslive.com/campaign/thenewbusy?ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_3




It might be some weird Windows thing. I know that some of the more recent 
versions of Windows, in an attempt to be more secure, make you jump through 
hoops to write to a file, even when the permissions are set correctly. 
Unfortunately, I don't have access to a system with IIS so there's no way I can 
test your code on that.


Well, my problem is not entirely solved, but I found that when I changed the 
file name extension from xml to txt, it is now letting me edit the file now. 
Only, the file is not writing in the way it is supposed to be. I used a+ 
originally, and it is writing outside my messages/messages root element. 

I tried w, and it wipes the file out. So, this proves that it is now modifying 
and saving it correctly. My question is, what kind of parameter should I use to 
add the things I would like to add after the messages without editing 
anything else in the file. 

Thanks for your help.

Alice







Thanks,

Ash

http://www.ashleysheridan.co.uk





  
_
Hotmail is redefining busy with tools for the New Busy. Get more from your 
inbox.
http://www.windowslive.com/campaign/thenewbusy?ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_2




There isn't any switch that suddenly recognises that you want to add nodes 
inside of an XML document, you have to write the code for that yourself.



There are two main ways to go about this:




Read in the entire file with file(), create a new array with your new 
content to insert, and splice() it back into the main array you read in at the 
start, then write the whole of that to the original file, overwriting anything 
in it.
Use XML functions to read in the contents of the file, add in the new nodes 
as you need, and overwrite the whole file again with the new DOM output



Either way, you're going to have to overwrite the existing file each time.

I ended up modifying the code by using r+ instead of using a, and modified your 
first option by reading the file line by line and writing out the lines over 
again plus the edits. Finally got it solved