Hey, I want to make a very simple CMS (Content Management System for those who haven't heard the acronym before) and I really don't have a clue where to start.

I want to be able to edit plain text files in say a textbox and then be able to click "save" and the contents of the text box written out to the file. I don't need sql, it's a very low content website.

I'm imagining the best way to do this is to read the file, output it into a text box, and on the click of the "save" button/link, output it back to the file ... now the last part seems like it's gonna be quite hard. If anyone has any ideas they would be most welcome 🙂

Thanks in advance

    You mean writing the text in your textbox to the file? That wouldn't be difficult at all...just put:

    fputs($filename, $text);

    Or were you refering to something else?

      For getting the data from the desired file into the HTML textarea:

      <textarea name="content">
      
      <?php
      $pf = fopen($file, "r");
      
      while(!feof($fp)){
      $text = fgets($fp, 4048);
      print $text;
      }
      ?>
      
      </textarea>
      

      Saving the new data:

      if(isset($_POST)){
      $fp = fopen($file, "w");
      $text = $_POST["content"];
      
      if(fputs($fp, $text)){
      print "Success";
      }
      else {
      print "Failure";
      }
      }
      

      Was that what you had in mind?

        thats fine thanks ... one thing, how do i assign the second bit of php to, say, a button?

          The part which saves the new data? Just put it at the top of your page source (somewhere above your <html> tag. Line 1 will do). A conditional checks whether if there is $_POST data available. If there is the script assumes that the user has pressed the submit button, then the script executes. Make sence?

          PS: You may want to put some validation in your app. Preferably JS validation, checking for empty fields etc.

            Replace the first script (the one that gets the data into the form) with this:

            <textarea>
            
            <?php
            if(isset($_GET["file"])){
            $file = $_GET["file"];
            
            $pf = fopen($file, "r");
            
            while(!feof($fp)){
            $text = fgets($fp, 4048);
            print $text;
            }
            }
            ?>
            
            </textarea>
            

            🙂

              Write a Reply...