file_put_contents() not over writing, just appending
<p>I have a simple <code>file.txt</code> which needs to be over written every time I run a certain php code.</p>

<pre><code>$msg ="string";
$file = 'file.txt';
$opn = file_get_contents($file);
$opn .= $msg;
file_put_contents($file, $opn);
</code></pre>

<p>The code works perfectly, It added the string I want it to add, but It is appending it to the former. I want it to erase it blank and then write.</p>

<p>I've read the manual <a href="http://php.net/manual/en/function.file-put-contents.php" rel="nofollow">http://php.net/manual/en/function.file-put-contents.php</a> 
I am not using the <code>FILE_APPEND</code> argument either.</p>

<p>It's a simple thing that I'm not seeing. Help me out.</p>

<p><strong>EDIT:</strong></p>

<p>Here is the whole code. I'm getting some data from a site. The data is in this format <code>12.222 AAA</code> and I am trying to save that to a text file to easily access later.
But for some reason something does not work. The scraper works perfectly, and the <code>file_put_contents()</code> woks perfectly if I set the <code>$item="string";</code> but it does not work when when <code>$item</code> is set to equal the code below. If I echo $item I get what I want.</p>

<pre><code>&lt;?php 

include('simple_html_dom.php');


$html = file_get_html('http://site.com/boom');

// find the thing in the span
foreach($html-&gt;find('div') as $article) {
    $item = $article-&gt;find('span', 0)-&gt;plaintext;

echo $item;

    $file = 'file.txt';
    $fh = fopen( 'file.txt', 'w' );
    fclose($fh);
    $opn = file_get_contents($file);
    $opn .= $item;
    file_put_contents($file, $opn);

}
?&gt;
</code></pre>
