Edit Text Using PHP problem

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

    Edit Text Using PHP problem

    I have a txtedit.php file. The source is below:
    Code:
    <?php
    
    $urlinf = "testfile.txt";
    
    $fh=$urlinf;
    $newtxt= $_POST['content'];
    if(isset($_POST['content']) AND ($_POST['content'])!=" ")
    {
    $content = stripslashes($newtxt);
    $fp= fopen($fh,"w") or die ("Error opening the file in write mode");
    fwrite($fp,$content);
    fclose($fp) or die ("Error closing file!");
    }
    
    ?>
    <h3><?php echo "Editing: ".$urlinf	?></h3>;
    <form name="txtedit" action="" method="post">
    
    <textarea style="font-size:12px" rows="25" cols="80" name="content">
    <?php @readfile ($fh) or die ("<h1>File does not exist</h1>");?></textarea>
    </br>
    <input type="submit" onclick="return confirm('Are you sure ?');" value="Save File"/> </form>
    Now This works fine but the problem is, whenever i edit the textfile, the lines increase. I cant explain this in words, so for example:

    The edited file text:
    Code:
    START
    HI
    SOMETEXT
    END
    When i save the file it becomes:
    Code:
    START
    
    HI
    
    SOMETEXT
    
    END
    And if i save again it becomes:
    Code:
    START
    
    
    HI
    
    
    SOMETEXT
    
    
    END
    And so on...
    After three or four edits, the line spaces become so much that the file becomes very hard to read or use.
    Is there any way around this problem?
    I have tried fwrite($fp,$content); and fputs($fp,$content); , but the problem still exists.
    Where is the problem? How do i solve it?
    Please help me
    Thanks in advance.
    Last edited by Sifat3d; 05.11.11, 17:26.

    #2
    i had a similar problem just at my script it was being tabs added not new lines and the way i solved it is that i replaced them with nothing using str_replace. you could try removing new lines, or use file_put_content function i never had problems with it.
    PHP Code:
    $("#mfreak").find(".head brain").clone(); 
    Progress:
    Code:
    [|||___________________________] : 5%
    Output:
    Code:
    Memory limit reached, unable to complete operation.
    Support answer:
    Code:
    Try using a super uber strong mega computer to reach at least 10%.

    Comment


      #3
      What do i replace? I didnt understand ur answer. Please anyone give me a solution.

      Comment


        #4
        Code:
         
        <?php
        $file = "testfile.txt";
        $canRead =  is_file($file) ? true : false;
        
        if($canRead && isset($_POST['content']) && $_POST['content'] != NULL)
        {
            $content = stripslashes($_POST['content']); 
            if (file_put_contents($file, $content))
                echo '<h3 style="background-color:pink;" align="center">File edited successfully!</h3>';
            else
                echo '<h3 style="background-color:pink;" align="center">Error while editing the file!</h3>';
        }
        ?>
        <h3><?php echo "Editing: ".$file; ?></h3>
        <form name="txtedit" action="" method="post">
            <textarea style="font-size:12px" rows="25" cols="80" name="content"><?php echo $canRead ? file_get_contents($file) : "File $file not found.";?></textarea>
              <br/>
            <input type="submit" onclick="return confirm('Are you sure?');" value="Save File"/> 
        </form>
        Last edited by i0nutzxp; 06.11.11, 06:23.
        <?php unlink('World/Europe/Romania.country'); ?>

        Comment

        Working...
        X