An error in a tutorial (a nice one)

I’m not claiming to be a great developer. look at this:

<?php
$xmlString = $HTTP_RAW_POST_DATA;
if (is_null($xmlString)) {
print “No data was sent”;
}
else {
$file = fopen(“guestbook.xml”, “w+”) or die(“Can’t open XML file”);
if(!fwrite($file, $xmlString)){
print “Error writing to XML-file”;
}
print $xmlString."
";
fclose($file);
}
?>

The tutorial goes on and talks about “The third ‘if’ statement writes to the file…”

Maybe that’s true, but there’s no thrid ‘if’ statement in this script. And that is why this script doesn’t work, this php script doesn’t Write to any file, but it DOES ERASE the file it’s supposed to be writing to.
Please recommend to your authors that smoking pot is ok, AFTER you proofread an upload.

the source file is “processXML.php”

oh well
mike

Giving the name or the URL of the tutorial would help.

its the guestbook tutorial

http://www.kirupa.com/web/xml_guestbook.htm

meatcake - you may want to contact the author directly about that. I’m willing to bet it was just a typo, for I and numerous others have not had any issues getting the guestbook to work using his instructions :blush:

I’m sorry for the tone in the post.
kirupies has taught me more than any other source anywhere.
I wrote this after about an hour of thinking I was insane for not getting it to work.
-mike

Hope you got it to work though :slight_smile: It’s a neat implementation of XML and Flash!

Have you posted in the forums about your problem? You can see that the example in the tutorial actually works so it might be something on your end.

Someone else had problems with it before and it was because of $HTTP_RAW_POST_DATA;

You might want to try replacing that with

get_file_contents(“php://input”);

And see if that helps at all.

Senoc - it is actually a small mistake in the tut.
there’s no fwrite()

but the $HTTP_RAW_POST_DATA might be only for php5 it’s not in the php.net dictionary.

theres an fwrite in the code you posted above

oh you mean this?

if(!fwrite($file, $xmlString)){
print “Error writing to XML-file”;
}
what’s the ’ ! ’ for ?
The script above does NOT write to an XML file, my friend.

agian though: is $HTTP_RAW_POST_DATA only in php version 5 ?
because I’m using 4 and there’s no such global. It’s not at www.php.net dictionary either.

and um,… what’s get_file_contents ???
you mean file_get_contents

mbyae i sohlud tpye all my cdoe lkie tihs it mgiht wrok tehn

I’m not sure that it works only in PHP 5 because the site and forums are only PHP 4.3.3. :slight_smile:

excuse me for the typo. You knew what I meant

And yes I meant that fwrite. The “!” is a not operator. It reverses a boolean. fwrite returns false if it was not successful (Im sure you could have looked this up at php.net to which you are so attached). So, that if statement checks to see if fwrite write was successful. If it wasnt you get a !false or true and the code block below is executed which conveniently returns “Error writing to XML-file”;

As I said before, its obviously working in the tutorial example so the code is not flawed. Something is just wrong on your end and you’ll need some debugging to figure it out. Maybe your permissions aren’t set right or you’ve mistyped a filename or maybe even your working with an older incompatible version of PHP - something, but the code, as you can see, works.