Web Analytics Made Easy -
StatCounter Editing a txt file via php - CodingForum

Announcement

Collapse
No announcement yet.

Editing a txt file via php

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Editing a txt file via php

    Hi,

    I am currently making a blog scrit for myself and have got it working all good at the moment, I was wanting to know is it is possible to edit a txt file via php? because i would want to be able to change entrys via the site rather then having to edit the txt file via ftp.

    Many thanks glen

  • #2
    PHP text editor using glob()

    Here's an example of a text editor I made a few months ago :-


    I recently made a text editor using the PHP glob functions to retrieve files and edit them.



    Here is the code:



    PHP Code:
    [B]scripteditor.php[/B]

    <form action="edit.php" method="post">

    Edit file:

    <select name="file">

    <?php

    // form to select file

    $files glob('*.html'); // get all html files

    $files2 glob('*.php'); // get all php files

    $files3 glob('*.txt'); // get all txt files

    for ($i 0$i sizeof($files); $i++){

    echo 
    '<option>' $files[$i] . '</option>';

    }

    for (
    $i 0$i sizeof($files2); $i++){

    echo 
    '<option>' $files2[$i] . '</option>';

    }

    for (
    $i 0$i sizeof($files3); $i++){

    echo 
    '<option>' $files3[$i] . '</option>'// add all files to the select menu

    }

    // Form to create new file

    ?>

    </select>

    <input type="submit" value="Load file" />

    </form>

    <form action="process.php?action=new" method="post">

    New file:<br>

    <input type="text" name="file" value="File Name">

    <br><input type="text" value="Password" name="pass" /><br>

    <input type="submit" value="New" />

    </form>



    [B]edit.php[/B]

    <a href="scripteditor.php"> Back </a>

    <?php

    if (!empty($_POST['file'])){

    $file $_POST['file'];

    }

    else {

    $file $_GET['file'];

    // Retieve the file

    ?>

    <form action="process.php?action=save" method="post">

    <textarea cols="100" rows="30" name="code"><?php echo file_get_contents($file);/* Get the file */ ?></textarea><br />

    <input type="hidden" name="file" value="<?php echo $file?>" />

    Save as a new file? <input type="checkbox" name="saveas" /> <br>

    <input type="text" value="File Name" name="filename" /><br>

    <input type="text" value="Password" name="pass" /><br>

    <input type="submit" value="Save" />

    </form>



    [B]process.php[/B]

    <?php

    if ($_POST['pass'] == 'password'){ // check the password

    if ($_GET['action'] == 'save'

    && $_POST['saveas'] == 'on'){ // get the action

    $fh fopen($_POST['filename'], 'x+'); // open the file and if it exists it will fail

    fwrite($fh$_POST['code']); // write new info to file

    header ('Location: edit.php?file=' urlencode($_POST['filename'])); // redirect back the edit.php

    }

    elseif (
    $_GET['action'] == 'save'){

    $fh fopen($_POST['file'], 'w+'); // open the file for writing

    fwrite($fh$_POST['code']); // write to file

    header ('Location: edit.php?file=' urlencode($_POST['file'])); // redirect back the edit.php

    }

    elseif (
    $_GET['action'] == 'new'){

    $fh fopen($_POST['file'], 'x+'); // create the file and if it exists it will fail

    header ('Location: edit.php?file=' urlencode($_POST['file'])); // redirect back the edit.php

    }

    }

    ?>

    Comment


    • #3
      I use an excellent textarea editor for txt files... as long as the php function magic_quotes is on it will save the file without any slashes and everything!

      PHP Code:
      <?php
      $file 
      "connect.txt"// insert your filename or url here
      if (!isset($_POST['submit']))
      {
        
      $fo fopen($file"r");
        
      $fr fread($fofilesize($file));
        if ( 
      get_magic_quotes_gpc () ) $fr stripslashes($fr);
        
        
      $fr str_replace("&""&amp;"$fr);
        
      $fr str_replace("<""&lt;"$fr);
        
      $fr str_replace(">""&gt;"$fr);
        
        echo 
      "<form method='post' action='{$_SERVER['PHP_SELF']}'>
              <textarea name='newfile' rows='10' cols='50'>
      {$fr}</textarea>
              <p>
              <input type='submit' name='submit' value='Save' />
              </form>"
      ;
        
      fclose($fo);
      }
      else
      {
        
      $fo fopen($file"w");
        
      $fw fwrite($fo, (get_magic_quotes_gpc()?stripslashes($_POST['newfile']):$_POST['newfile']));
        
      fclose($fo);
      }
      ?>
      Everyone hears what you say, friends listen to what you say, best friends listen to what you don't say.
      Radio DJ Panel v3 - It's Here!

      Comment


      • #4
        Thanks alot for your help much apreciated!

        Ill let you guys know when im completed.

        Thanks glen

        Comment


        • #5
          Sorry for double post,

          But i have another question, when i write a entry to the txt file via my script it all works fine, but when i add another parts of my older entrys are disapperaing and everything is messing up, here is my wirtefile.php code

          Code:
          <?php   
          
          // FILENAME - writefile.php  
          
          if ($_POST['submit'] == "Submit") {  
              $dir = 'entrys/';   
              $filename = "content.txt";  
          
              $content = $_POST['name']."&nbsp;&nbsp; - &nbsp;&nbsp;";   
              $content .= $_POST['email']."<br><b>";  
              $content .= date('r')."<br></b>"."<br>";
          	$content .= $_POST['entry']."<hr>";
          	 
          
              if (!$handle = fopen($dir . $filename, "r+")) {   
                  echo 'Cannot open file: ' . $filename;   
                  exit;   
              }   
              if (!fwrite($handle, $content)) {   
                  echo 'Cannot write to file: ' . $filename;   
                  exit;   
              }   
          
              echo '<head><style type="text/css">
          td img {display: block;}body {
          	background-image: url(background.gif);
          }
          </style></head><center>Would you like to add another? <a href="../blog/add.php">Yes</a> - <a href="../blog/index.php">No</a> <br></center>' . "<br><center>The entry was written to $dir $filename</center>" . '<center><br> <a href="http://www.hughesy1986.com">Hughesy1986 Blog Script</a></center>';  
          } else {  
              echo "<meta http-equiv=\"refresh\" content=\"0; url=/blog/add_confrim.php\" />\n";
          }  
          
          ?>
          can anyone see where i am going wrong? because i think it is the html i am placing in after the posts.

          Any help would be most appreciated thanks glen

          Comment


          • #6
            hmmmm....

            your syntax looks generally ok, mind you I don't quite understand your problem? my only suggestion until you can clarify is... make sure your content.txt file is CHMOD'ed to 0777.

            WAIT (edit) - k, the problem lies in your echo function (i think) you must never put any " inside a function.... especially an echo function, try changing that to ' instead.

            good luck
            Everyone hears what you say, friends listen to what you say, best friends listen to what you don't say.
            Radio DJ Panel v3 - It's Here!

            Comment


            • #7
              Well ive got most of this working now, there are a couple of things which are bothering me but all is good! Thanks alot for your help much appreciated,

              You can view the site herehttp://www.hughesy1986.com/blog1

              Comment

              Working...
              X