# Exporting to a textfile (or any other if better)

**URL:** <https://forum.kirupa.com/t/exporting-to-a-textfile-or-any-other-if-better/38588>\
**Category:** programming\
**Created:** [December 26, 2003, 5:52am UTC](https://forum.kirupa.com/t/exporting-to-a-textfile-or-any-other-if-better/38588 "2003-12-26T05:52:56Z")\
**Posts on this page:** 1\
**Showing post:** 20

<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:** [December 27, 2003, 7:30am UTC](https://forum.kirupa.com/t/exporting-to-a-textfile-or-any-other-if-better/38588/20 "2003-12-27T07:30:58Z")

</div>

```php
<?php
//return var 'news'
$filename = 'news.txt';
$somecontent = stripslashes($_POST['news']);

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

    if (!$handle = fopen($filename, 'w')) {
         print "news=Cannot open file ($filename)";
         exit;
    }

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

    if (!$handle = fopen($filename, 'a')) {
         print "news=Cannot open file ($filename)";
         exit;
    }

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

    print "news=$somecontent";
    
    fclose($handle);
    
}
    
    print "news=$somecontent";
                    
} else {
    print "news=The file $filename is not writable";
}
?>

```

---

_[View the full topic](https://forum.kirupa.com/t/exporting-to-a-textfile-or-any-other-if-better/38588)._
