title: Not writing (or even opening file) in php
body: <p>I read many of the other questions on this site revolving around this error, none of which were able to help me, The below php code takes the data input from the html file and sends it to the php file, after printing "Hello Name" on the page, it should open a file called My_file.txt, which is already created, and write to it, I have been unable to do so, any help would be great, here is my code:</p>
  
  <p>index.html file:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;&lt;title&gt;Hello Friend&lt;/title&gt;&lt;/head&gt;
  &lt;body bgcolor="black"&gt;
  &lt;p style="text-align:center"&gt;
  &lt;font color="white"&gt;
  &lt;form action="data.php" method="post"&gt;
      &lt;input type="text" name="myname" id="myname"&gt;
      &lt;input type="submit" value="Submit"&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>data.php file:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Hello Friend, this is php&lt;/title&gt;
  &lt;/head&gt;
  &lt;body bgcolor="black"&gt;
  &lt;font color="white"&gt;
  &lt;code&gt;
  &lt;?php
      $name = $_POST["myname"];
      echo "Hello, " . $name;
      $filename = "My_file.txt";
      chmod($filename, 777);
      $f = fopen($filename, "w") or die("Unable to open file");
      fwrite($f, "The Name is: " . $name);
      f.close($f);
      echo fgets($f);
      fclose($f);
  ?&gt;
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Hello, JohnUnable to open file
  </code></pre>