# \[PHP\] fclose help

**URL:** <https://forum.kirupa.com/t/php-fclose-help/56578>\
**Category:** programming\
**Created:** [July 5, 2004, 9:54pm UTC](https://forum.kirupa.com/t/php-fclose-help/56578 "2004-07-05T21:54:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![joso](https://avatars.discourse-cdn.com/v4/letter/j/b3f665/32.png) [@joso](https://forum.kirupa.com/u/joso)\
**Post date:** [July 5, 2004, 9:54pm UTC](https://forum.kirupa.com/t/php-fclose-help/56578/1 "2004-07-05T21:54:45Z")

</div>

hi…

i’m trying to sort out this text area that adds text to a textfile called test.txt…im just using the one text box so i want it so when you hit enter to start a new line in the text box it inserts a linebreak when you’ve submitted the text into the textfile…

i also want each post to just wipe over the last…so theres only ever one lot of text in the text file at a time…🙂 thanks

heres the code im using for the php file to add text:

```php
<?php
$filename = 'test.txt';
$somecontent = '<table width="90" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="6" align="right" valign="middle"></td>
    <td width="84" align="left" valign="top"><strong>' . date('d.m.y') . '</strong></td>
  </tr>
  <tr>
    <td align="right" valign="top">-</td>
    <td align="left" valign="top">' . nl2br($_POST['updates']) . '</td>
  </tr>
</table>';

if (is_writable($filename)) {
   if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
   }
   if (fwrite($handle, $somecontent) === FALSE) {
       echo "Cannot write to file ($filename)";
       exit;
   }
   
   echo "Success, Updates were changed!";
   
   fclose($handle);
                   
} else {
   echo "The file $filename is not writable";
}
?>

```

and if you want to see the text box…its here :

[http://www.band-central.net/atreyu/test/index.php](http://www.band-central.net/atreyu/test/index.php)

and the post gets added to this page

[http://www.band-central.net/atreyu/home.php](http://www.band-central.net/atreyu/home.php)

on the left side in the ‘updates’ box

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 6, 2004, 2:21am UTC](https://forum.kirupa.com/t/php-fclose-help/56578/2 "2004-07-06T02:21:45Z")

</div>

maybe this will help

```php

$text_post = nl2br($somecontent);

```

use $text in the fwrite(…) function instead of $somecontent

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 6, 2004, 5:24am UTC](https://forum.kirupa.com/t/php-fclose-help/56578/3 "2004-07-06T05:24:31Z")

</div>

or change this line  
\<td align=“left” valign=“top”\>’ . ($updates) . ‘\</td\>  
to  
\<td align=“left” valign=“top”\>’ . nl2br($\_POST[‘updates’]) . '\</td\>

try to use the $\_POST array instead of relying on register\_globals being ‘on’ in php.ini. That way your script will still work if you one day have to move it to another server or give it to someone.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 6, 2004, 12:57pm UTC](https://forum.kirupa.com/t/php-fclose-help/56578/4 "2004-07-06T12:57:10Z")

</div>

ok…thats worked, one more thing…

how would i get the posts to list in order of the posted, newest-oldest…with only two being posted, then the oldest one gets deleted when a new one is posted 🙂

thanks

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 6, 2004, 4:37pm UTC](https://forum.kirupa.com/t/php-fclose-help/56578/5 "2004-07-06T16:37:30Z")

</div>

That’ll be hard because you store your items as HTML. You have to read the entire file and somehow separate each entry from the others. Then you have to output the last two in reverse order.

If you’d stored them in the file without the HTML - or in a database - it’d be much easier.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 6, 2004, 5:06pm UTC](https://forum.kirupa.com/t/php-fclose-help/56578/6 "2004-07-06T17:06:25Z")

</div>

whatabout just storing them in reverse order? would that be possible still storing them as html?

i have no clue when it comes to databases ☹

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 6, 2004, 8:31pm UTC](https://forum.kirupa.com/t/php-fclose-help/56578/7 "2004-07-06T20:31:30Z")

</div>

If you store them as HTML in reverse order, you still have to figure out where the second entry ends, so you can chop it off there.

You could put some sort of marker between the entries. An HTML comment for instance. But it’s still a bit of a kludge, IMO.
