0

Hi,

I have the code as in the following:

<?php

$name= "Test";
$phone= "123-456-7890";
$email = "myemail@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@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.

Edited by ajwei810192: n/a

2
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by ajwei810192
0

For starters, you are opening the file with an 'a' which is write only then you are trying to read it and that should fail. It seems that you are trying to insert into the file rather than append. If that is the case, you could try another option like r+ or a+ but I don't know if that will do it. It might overwrite (for an r+). You may need to read the file and rebuild it to a string and then re-write the whole thing. I also wonder if your if (trim($line) == "<messages>") { will work because the line is actually terminated by a new line character. You may have to check for a substring.

0

For starters, you are opening the file with an 'a' which is write only then you are trying to read it and that should fail. It seems that you are trying to insert into the file rather than append. If that is the case, you could try another option like r+ or a+ but I don't know if that will do it. It might overwrite (for an r+). You may need to read the file and rebuild it to a string and then re-write the whole thing. I also wonder if your if (trim($line) == "<messages>") { will work because the line is actually terminated by a new line character. You may have to check for a substring.

I did check the permission of the folder I was working with, and looks like that my IIS did have write, modify, read and execute permissions. I took your advice, and edited the I/O operator from "a" to "w+", and looks like it did not work. As a matter of fact, it looks like the time and date on that file I was attempting to access was only accessed and not "modified".

Is there anything else I missed here?
Thanks for your help.

0

If the echo on line 22 isn't writing anything to the output then it probably means that your If statement on line 19 isn't working. Please read my previous post again.

0

I fixed the problem with editing the file by using

$fh = fopen($file,"r+");

instead of

$fh = fopen($file,"a");

I also took your advice by echoing out all the lines and replaced the desired replacements, and looks like it is working now.

Thanks.

Edited by ajwei810192: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.