title: Php error: "Filename cannot be empty"
body: <p>I'm creating a site that let's the user put in information and later save it to a document on the computer using PHP read/write. Everything is working fine except I'm getting two errors. </p>
  
  <p>The errors are "Undefined offset: 6 in C:\wamp\www\Phoenix\ReadWrite.php on line 109" and " fopen(): Filename cannot be empty in C:\wamp\www\Phoenix\ReadWrite.php on line 110".
  This is the code:</p>
  
  <pre><code>&lt;?php
  $nameErr = $ssnErr = $dateErr = $timeErr = $fileErr ="";
  $name = $ssn = $date = $time = $comment = $file = "";
  
  if ($_SERVER["REQUEST_METHOD"] == "POST") {
     if (empty($_POST["1"])) {
       $nameErr = "Name is required";
       $_POST['6'] = "";
     } else {
       $name = test_input($_POST["1"]);
     }
  
     if (empty($_POST["2"])) {
       $ssnErr = "SSN is required";
       $_POST['6'] = "";
     } else {
       $ssn = test_input($_POST["2"]);
     }
  
     if (empty($_POST["3"])) {
       $dateErr = "Date is required";
       $_POST['6'] = "";
     } else {
       $date = test_input($_POST["3"]);
     }
  
     if (empty($_POST["4"])) {
       $timeErr = "Time is required";
       $_POST['6'] = "";
     } else {
       $time = test_input($_POST["4"]);
     }
  
     if (empty($_POST["5"])) {
       $comment = "";
     } else {
       $comment = test_input($_POST["5"]);
     }
  
     if (empty($_POST["6"])) {
       $fileErr = "Filename is required";
     } else {
       $file = test_input($_POST["6"]);
     }
  }
  
  function test_input($data) {
     $data = trim($data);
     $data = stripslashes($data);
     $data = htmlspecialchars($data);
     return $data;
  }
  ?&gt;
  
  &lt;form method="post" action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;"&gt; 
  &lt;table width="500" border="0" cellspacing="5" cellpadding="0"&gt;
    &lt;tr&gt;
      &lt;td scope="col"&gt;Name:&lt;/td&gt;
      &lt;td colspan="2" scope="col"&gt;&lt;input type="text" name="1"&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span class="error"&gt;&lt;?php echo $nameErr;?&gt;&lt;/span&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;SSN:&lt;/td&gt;
      &lt;td colspan="2"&gt;&lt;input type="text" name="2"&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span class="error"&gt;&lt;?php echo $ssnErr;?&gt;&lt;/span&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Date:&lt;/td&gt;
      &lt;td colspan="2"&gt;&lt;input type="text" name="3"&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span class="error"&gt;&lt;?php echo $dateErr;?&gt;&lt;/span&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Time of Arrival:&lt;/td&gt;
      &lt;td colspan="2"&gt;&lt;input type="text" name="4"&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span class="error"&gt;&lt;?php echo $timeErr;?&gt;&lt;/span&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Comment:&lt;/td&gt;
      &lt;td colspan="2"&gt;&lt;textarea rows="7" cols="30" name="5"&gt;&lt;/textarea&gt;&lt;/td&gt;
      &lt;td&gt;&amp;nbsp;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Filename:&lt;/td&gt;
      &lt;td colspan="2"&gt;&lt;input type="text" name="6"&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span class="error"&gt;&lt;?php echo $fileErr;?&gt;&lt;/span&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;/td&gt;
      &lt;td&gt;&lt;input type="submit" name="submit" value="Submit"&gt; &lt;/td&gt;
      &lt;td&gt;&lt;input type="reset" name="reset" value="Reset" &gt;&lt;/td&gt;
      &lt;td&gt;&amp;nbsp;&lt;/td&gt;
    &lt;/tr&gt;  
  &lt;/table&gt;
  &lt;/form&gt;
  
  &lt;?php
  $file = $_POST['6'];
  $myfile = fopen($file, "a") or die("Unable to open file!");
  $one = "\r\n" . $_POST['1'] . "\r\n";
  fwrite($myfile, $one);
  $two = $_POST['2'] . "\r\n";
  fwrite($myfile, $two);
  $three =$_POST['3'] . "\r\n";
  fwrite($myfile, $three);
  $four =$_POST['4'] . "\r\n";
  fwrite($myfile, $four);
  $five = $_POST['5'] . "\r\n";
  fwrite($myfile, $five);
  fclose($myfile);
  ?&gt;
  </code></pre>
  
  <p>My question is: how can i solve the problem with the empty field in my form? </p>