title: How to edit/update a txt file with php
body: <p>After I've read a lot of similar problems with the edit/update function on a file and none of it worked I would like to ask for some help.</p>
  
  <p>I am trying to edit a <code>.txt</code> document from php. I have tried these things:</p>
  
  <ol>
  <li><p>This was the last code which I've read here and didn't work.</p>
  
  <pre><code>$data_to_write = "$_POST[subject]";
  $file_path = "text/" + $row['name'];
  $file_handle = fopen($file_path, 'w'); 
  fwrite($file_handle, $data_to_write);
  fclose($file_handle);
  </code></pre></li>
  <li><p>And this is my previous try:</p>
  
  <pre><code>$new_contents = "$_POST[subject]\n";
  $path = "text/$row[name]";
  file_put_contents($path, $new_contents);
  </code></pre></li>
  </ol>
  
  <p>I hope someone would explain me how to do this the right way. Thank you.</p>
  
  <p>This is all of my code: </p>
  
  <pre><code>&lt;?php
  if(isset($_GET['id']))
  {
  $edit = mysql_query("SELECT * FROM text_area WHERE text_id=$_GET[id]");
  $row = mysql_fetch_assoc($edit);
  $contents = file_get_contents($row['content']);
  ?&gt;
  &lt;form action="" name="form" method="post"&gt;
  &lt;input type="hidden" name="id" value="&lt;?php echo $row['text_id']; ?&gt;" /&gt;&lt;br /&gt;
  &lt;label for=""&gt;Заглавие:&lt;/label&gt;&lt;br /&gt;
  &lt;input type="text" name="title" style="width:500px;" value="&lt;?php echo $row['subject'] ?&gt;" /&gt;&lt;br /&gt;
  &lt;select name="opt"&gt;
  &lt;option value="0"&gt;&lt;/option&gt;
  &lt;?php
  
  $result = mysql_query("SELECT * FROM image_area");
  while ($row = mysql_fetch_array($result))
  {
          echo "&lt;option value=" . $row['path'] . "&gt;" . $row['name'] . "&lt;/option&gt;
              ";  
      }
  
  ?&gt;
  &lt;/select&gt;&lt;input type="button" name="sumbitP" value="Choose" onclick="addtext();" /&gt;&lt;a href="../image_list.php" target="_blank"&gt;Image list&lt;/a&gt;&lt;br /&gt;
  &lt;textarea rows="10" cols="50" name="text" id="markItUp"&gt;&lt;?php echo $contents ?&gt;&lt;/textarea&gt;&lt;br /&gt;
  &lt;input type="submit" name="sumbitT" value="Update" /&gt;
  &lt;input type="reset" value="Reset" /&gt;
  
  &lt;/form&gt;
  &lt;?php
  }
  ?&gt;
  &lt;?php
  
  if(isset($_POST['id']))
  {
      if(mysql_query("UPDATE text_area SET title='$_POST[subject]' WHERE text_id ='$_POST[id]'"))
      {
  
  $data_to_write = "" . $_POST['text'];
  $file_path = "text/$row[name]";
  $file_handle = fopen($file_path, 'w');
  fwrite($file_handle, $data_to_write);
  fclose($file_handle);
  
          echo '&lt;br&gt;&lt;br&gt;&lt;p align="center"&gt;Everything is ok&lt;/p&gt;';
      } else {
          echo '&lt;br&gt;&lt;br&gt;&lt;p align="center"&gt;Everything is not ok&lt;/p&gt;' ;
      }
  ?&gt;
  </code></pre>
  
  <p>Just to add something which might be useful: <br> I am getting this error which I can't manage to find an answer for with Google. <br>
  <code>Warning: fopen(text/) [function.fopen]: failed to open stream: Is a directory in</code></p>