fopen, fprint, fclose problems..

A

Anonymous

Guest
Hi gang!

First of all, I have read everything I can find on these procedures, but they all say the same thing... re: 'opening, writing to a file and then closing the file. Unfortunately, they don't say what file types can be written with this method. All the examples I have found ONLY write to text (*.txt) files. I'm trying to do something a little different. Maybe I'm going about it the wrong way... I'm not sure. But, here it is:::
:help:
I'm trying to make a simple PHP guestbook that will write table rows to an HTML page instead of a TEXT file. I'm using a php to do the <table>... header, an include of the following file's output, and then the </table> footers.

Here's the PHP I'm having trouble with.

Code:
<?
$todaysDate=date("l, M jS, Y");
$todaysTime=date("g:i:s A - T");
$carriageReturn = "<br>";
$recordNumber = $recordNumber +1;

// Opens read.html file for writing.

$FilePointer=@fopen(read.html,"a");

// Creates record
$message = "<tr><br>";
$message .= "<td>Record Number</td><br>";
$message .= "<td>$recordNumber</td><br>";
$message .= "</tr><br>";
$message .= "<tr><br>";
$message .= "<td>Date:  $todaysDate</td><br>";
$message .= "<td>Time:  $todaysTime</td><br>";
$message .= "</tr><br>";
$message .= "<tr><br>";
$message .= "<td colspan=2>Name:  $writerName (<a href=\"mailto:$writerEmail?subject=Guestbook Question or Comment:\">$writerEmail</a>)</td><br>";
$message .= "</tr><br>";
$message .= "<tr><br>";
$message .= "<td colspan=2>Message:  $writerMessage</td><br>";
$message .= "</tr><br>";
$message .= "<tr>";
$message .= "<td colspan=2><hr color=#C0C0C0></td><br>";
$message .= "</tr><br>";

// writes record to read.html file
@fwrite($FilePointer,$message);

// closes read.html file
@fclose($FilePointer);

// send email to us telling who and what was written to the guestbook for safety.
$mailto = "me@mydomain.com";
$subject = "I have just written to the guestbook!";

mail($mailto, $subject, $writerMessage, "From: $writerEmail\r\n" . "Reply-To: $writerEmail\r\n");

print ("Thank you for your submission!<br>
<br>
Your comments are <b>VITAL</b> to making this site better!<br>
");

?>

In theory, it should work perfectly. It is really not writing to the file at all.

Am I going about this in the right way? Am I missing something, here? Can the same thing be accomplished more efficiently?

ANY help you can give me would be GREATLY appreciated as this is really confusing & I don't want to resort to using a text file with the guestbook.

Thank you in advance & take care,

B.
 
WRStrong said:
Code:
$FilePointer=@fopen(read.html,"a");

For starters, when posting questions, you should post the errors you're getting. In this case I'm guessing that you're getting a few, but if not, then you should change error_reporting in php.ini to E_ALL.

The trouble with the above code is that the name of the file should be a string, which means that it needs to be between quotation marks.
 
Swirlee, thanks for the help.

I never received any error messages before, it just didn't do anything other than print the text at the bottom.

Anyhow, I've changed the code.... don't understand how, but it works!

Code:
<?php
$filename = 'read.php';

$todaysDate=date("l, M jS, Y");
$todaysTime=date("g:i:s A - T");
$carriageReturn = "";
$recordNumber = $recordNumber +1;

$message = "<tr>";
$message .= "<td><font size=1>Record Number:  $recordNumber</font></td>";
$message .= "</tr>";
$message .= "<tr>";
$message .= "<td><font size=1>Date:  $todaysDate</font></td>";
$message .= "<td><font size=1>Time:  $todaysTime</font></td>";
$message .= "</tr>";
$message .= "<tr>";
$message .= "<td colspan=2><font size=1>Name:  $writerName (<a href=\"mailto:$writerEmail?subject=Guestbook Question or Comment:\">$writerEmail</a>)</font></td>";
$message .= "</tr>";
$message .= "<tr>";
$message .= "<td colspan=2><font size=1>Message:  $writerMessage</font></td>";
$message .= "</tr>";
$message .= "<tr>";
$message .= "<td colspan=2><hr color=#C0C0C0></td>";
$message .= "</tr>";

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

    // In our example we're opening $filename in append mode.
    // The file pointer is at the bottom of the file hence
    // that's where $somecontent will go when we fwrite() it.
    if (!$handle = fopen($filename, 'a')) {
         print "Cannot open file ($filename)";
         exit;
    }

    // Write $message to our opened file.
    if (!fwrite($handle, $message)) {
        print "Cannot write to file ($filename)";
        exit;
    }

    // send email to us telling who and what was written to the guestbook for safety.
$mailto = "me@you.com";
$subject = "I have just written to the guestbook!";


mail($mailto, $subject, $writerMessage, "From: $writerEmail\r\n" . "Reply-To: $writerEmail\r\n");

?>

Can you explain what is going on with part of the code? php.com is kinda vague & I dont understand HOW it works. (I'm talking about expressions like ' if (!fwrite($handle, $message)) ' above. It's an 'if' statement, right? How can it write if it's not directly told to do so? And what's the '!' for before the fwrite command?

Please point me in the right direction.

Thanks for all the help,

B.
 
if (!fwrite($handle, $message))

the ! means if it is NOT this...

and by putting a function inside an if(), it automatically executes this code. I think the only real reason to do it this way is to save time and confusing yourself with hundreds of variables.

Hope that helps slightly... I'm sure someone else can explain better than me!

Andrew
 
Back
Top