From: Alice Wei on

Hi,

I have the code as in the following:

<?php

$name= "Test";
$phone= "123-456-7890";
$email = "myemail(a)mymail.com";
$comments = "Test Only";

$string = "<message>\n<name>" . $name . "</name>\n<phone>" . $phone . "</phone>\n<email>".
$email . "</email>\n<comments>" . $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>
<name>Me</name>
<phone>123-456-7890</phone>
<email>test(a)test.com</email>
<comments>This is my message board!</comments>
</message>
</messages>

Have I missed something here? How come the file cannot perform fwrite actions?

Thanks for your help.

_________________________________________________________________
The New Busy is not the too busy. Combine all your e-mail accounts with Hotmail.
http://www.windowslive.com/campaign/thenewbusy?tile=multiaccount&ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_4
From: Ashley Sheridan on
On Sun, 2010-06-27 at 07:20 -0400, Alice Wei wrote:

> Hi,
>
> I have the code as in the following:
>
> <?php
>
> $name= "Test";
> $phone= "123-456-7890";
> $email = "myemail(a)mymail.com";
> $comments = "Test Only";
>
> $string = "<message>\n<name>" . $name . "</name>\n<phone>" . $phone . "</phone>\n<email>".
> $email . "</email>\n<comments>" . $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>
> <name>Me</name>
> <phone>123-456-7890</phone>
> <email>test(a)test.com</email>
> <comments>This is my message board!</comments>
> </message>
> </messages>
>
> Have I missed something here? How come the file cannot perform fwrite actions?
>
> Thanks for your help.
>
> _________________________________________________________________
> The New Busy is not the too busy. Combine all your e-mail accounts with Hotmail.
> http://www.windowslive.com/campaign/thenewbusy?tile=multiaccount&ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_4


I assume you're opening the file in a text editor and you've not found
what you expect to be there?

Have you made sure that Apache has permission to write to the file? If
you created the file yourself and uploaded it, chances are it doesn't
have the right permissions.

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


From: tedd on
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 = "myemail(a)mymail.com";
>$comments = "Test Only";
>
>$string = "<message>\n<name>" . $name . "</name>\n<phone>" . $phone
>. "</phone>\n<email>".
> $email . "</email>\n<comments>" . $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>
><name>Me</name>
><phone>123-456-7890</phone>
><email>test(a)test.com</email>
><comments>This 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

--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
From: Alice Wei on

> Subject: Re: [PHP] Modifying Existing Text File From PHP Is Not Working
> From: lexsimon(a)gmail.com
> Date: Sun, 27 Jun 2010 13:47:53 +0200
> To: ajwei(a)alumni.iu.edu
>
> You are closing your file too early. Close it outside the foreach.
> An other point : you should not 'edit' an XML file by this way ; use an XML api like dom or simplexml
>
> Regards
>
> --
> Alexandre Simon
> http://alex.zybar.net
>

Hi,

I did try changing the fclose to after all the brackets in my php file. On another note, I checked the permission of my folder that I was attempting to write in, which all users have write, modify, read and execute permissions. I don't see any 500 or permission denied errors, and I use IIS, so I don't use apache.

It didn't work though, the suggestion you provided on simpleXML looks promising, and yet I am writing not to the child, and plus I seem to be getting a 500 error. Another thing is, do I need to "save" the file after all this echoing is done using simpleXML?

BTW, here is the code, which I am sure that I must have done something wrong here. Could anyone please help me out here?

Thanks for your help.
Is it possible that I could do it the way I did it before by any chance?

<?php

$name= "Test";
$phone= "123-456-7890";
$email = "mymail(a)mail.com";
$comments = "Test Only";

//If file exists. append, otherwise create
$file = "messages.xml";

$xml = simplexml_load_file($file);
$xml-> messages[0]->addChild("message", "");
$xml -> message[0]->addChild("name",$name);
$xml -> message[0] ->addChild("phone",$phone);
$xml -> message[0] -> addChild("email",$email);
$xml-> message[0] -> addChild("comments",$comments);

echo $xml->asXML();

?>

Alice

> Le 27 juin 2010 à 13:20, Alice Wei <ajwei(a)alumni.iu.edu> a écrit :
>
> >
> > Hi,
> >
> > I have the code as in the following:
> >
> > <?php
> >
> > $name= "Test";
> > $phone= "123-456-7890";
> > $email = "myemail(a)mymail.com";
> > $comments = "Test Only";
> >
> > $string = "<message>\n<name>" . $name . "</name>\n<phone>" . $phone . "</phone>\n<email>".
> > $email . "</email>\n<comments>" . $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>
> > <name>Me</name>
> > <phone>123-456-7890</phone>
> > <email>test(a)test.com</email>
> > <comments>This is my message board!</comments>
> > </message>
> > </messages>
> >
> > Have I missed something here? How come the file cannot perform fwrite actions?
> >
> > Thanks for your help.
> >
> > _________________________________________________________________
> > The New Busy is not the too busy. Combine all your e-mail accounts with Hotmail.
> > http://www.windowslive.com/campaign/thenewbusy?tile=multiaccount&ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_4

_________________________________________________________________
The New Busy is not the too busy. Combine all your e-mail accounts with Hotmail.
http://www.windowslive.com/campaign/thenewbusy?tile=multiaccount&ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_4
From: Alice Wei on


> Date: Sun, 27 Jun 2010 12:01:50 -0400
> To: ajwei(a)alumni.iu.edu; php-general(a)lists.php.net
> From: tedd.sperling(a)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 = "myemail(a)mymail.com";
> >$comments = "Test Only";
> >
> >$string = "<message>\n<name>" . $name . "</name>\n<phone>" . $phone
> >. "</phone>\n<email>".
> > $email . "</email>\n<comments>" . $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>
> ><name>Me</name>
> ><phone>123-456-7890</phone>
> ><email>test(a)test.com</email>
> ><comments>This 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