Jump to content

write a php ost to a file


MadnessRed

Recommended Posts

ok, very simple

 

I want to write 3 blank lines then this to the end of comments.txt

 

echo date("l jS F, Y - h:i");
echo "<br>";
echo $_POST['name'];
echo " - (";
echo getenv('REMOTE_ADDR');
echo ")";
echo "<br><br>";
echo $_POST['message'];
echo "<br><hr><br>";

 

I know I am missing something obvious here but I wont work for me

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/97873-write-a-php-ost-to-a-file/
Share on other sites

sorry forgot

 

my code atm

 

<?php
$filename = 'comments.txt';
$somecontent = "
echo date("l jS F, Y - h:i");
echo "<br>";
echo $_POST['name'];
echo " - (";
echo getenv('REMOTE_ADDR');
echo ")";
echo "<br><br>";
echo $_POST['message'];
echo "<br><hr><br>";
\n\n\n
";

// 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')) {
         echo "Cannot open file ($filename)";
         exit;
    }

    // Write $somecontent to our opened file.
    if (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }

    echo "Success, wrote ($somecontent) to file ($filename)";

    fclose($handle);

} else {
    echo "The file $filename is not writable";
}
?>

That code will give you errors and will not work.

 

I assume you want to put everything into the "$somecontent" variable:

<?php
$tmp = array();
$tmp[] = date("l jS F, Y - h:i");
$tmp[] = "<br>";
$tmp[] = $_POST['name'] . " - (" . getenv('REMOTE_ADDR') .")";
$tmp[] =  "<br><br>";
$tmp[] = $_POST['message'];
$tmp[]  = "<br><hr><br>";
$somecontent = implode("\n",$tmp) . "\n\n\n";
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.