Hello everyone! I am trying to build something for a backend and I'm not sure how to do it.

I need to have a text area where an operator can put in some text, click the submit button and it stores in a text file. I will then use an include to bring the entire contents of the .txt file as the alt text for that box.

I want operators to be able to freely add text to the .txt file, without deleting previous comments.

Thank you guys in advance. I really appreciate any help! 😃

    1. To write to a file, all you need to do is call [man]fopen/man (probably with the 'w' flag in this case) and use [man]fwrite/man to write data to the file.

    2. To retrieve the previously stored comments, you'd do something like this:

      $data = file_get_contents('file.txt');
      echo '<textarea name="whatever">' . htmlspecialchars($data) . '</textarea>';
    3. I would strongly advise against using [man]include[/man] to display the contents of the .txt file -- this would allow PHP code to be executed on the server!

      Thank you sooo much for your response.

      Would you mind telling me how to write to the txt file?

      This is what I have, but I can't figure out how to get the text field to write in there:

      <?php
      $filename = 'test.txt';
      $message = wordwrap($_POST['Message'], 70);
      
      // Let's make sure the file exists and is writable first.
      if (is_writable($filename)) {
      
         // In our example we're opening $filename in append mode.
         // The file pointer is at the bottom of the file hence
         // that's where $somecontent will go when we fwrite() it.
         if (!$handle = fopen($filename, 'a')) {
               echo "Cannot open file ($filename)";
               exit;
         }
      
         // Write $somecontent to our opened file.
         if (fwrite($handle, $somecontent) === FALSE) {
             echo "Cannot write to file ($filename)";
             exit;
         }
      
         echo "Success, wrote ($somecontent) to file ($filename)";
      
         fclose($handle);
      
      } else {
         echo "The file $filename is not writable";
      }
      ?>

      So I would need something that would "POST" to itself or something, right?

        Well, first of all, $somecontent should be changed to $message since that's where the POSTed data is. Also, I would change the file mode on the fopen() call to 'w' as I said earlier... otherwise it'll just keep adding the data posted onto the end of the file, rather than replacing the file's contents.

        To answer your last question there, yes, you would be POSTing data to this script.

          Obviously, I don't know what I'm doing. This isn't right, is it?

          <?php
          $filename = 'test.txt';
          $message = wordwrap($_POST['Message'], 70);
          
          $data = file_get_contents('file.txt');
          echo '<textarea name="whatever">' . htmlspecialchars($data) . '</textarea>'; 
          
          // Let's make sure the file exists and is writable first.
          if (is_writable($filename)) {
          
             // In our example we're opening $filename in append mode.
             // The file pointer is at the bottom of the file hence
             // that's where $somecontent will go when we fwrite() it.
             if (!$handle = fopen($filename, 'a')) {
                   echo "Cannot open file ($filename)";
                   exit;
             }
          
             // Write $somecontent to our opened file.
             if (fwrite($handle, $somecontent) === FALSE) {
                 echo "Cannot write to file ($filename)";
                 exit;
             }
          
             echo "Success, wrote ($message) to file ($filename)";
          
             fclose($handle);
          
          } else {
             echo "The file $filename is not writable";
          }
          ?> 
          

          And where should I put the submit button and POST functions? :queasy: Sorry.

            Write a Reply...