I want a webpage that can open another webpage, edit the contents and then save the changes.

I can do this using the functions fread and fwrite, however the pages I'm trying to edit contain both html and php.

I use fread then put the contents into a <textarea>, the problem is that the files I'm editing contain the tag </textarea> so at that point the contents of the file are no longer displayed in the text area - some lines go missing and the rest is displayed outside the textarea.

I've tried various ideas including stripslashes and htmlentities to no avail.

<?php 
// set file to read 
$filename = "testfile.php"; 

$newdata = $_GET['newdata']; 

if ($newdata != '') { 

// open file  
$fw = fopen($filename, 'w') or die('Could not open file!'); // write to file // added stripslashes to $newdata $fb = fwrite($fw,stripslashes($newdata)) or die('Could not write
to file'); // close file fclose($fw); } // open file $fh = fopen($filename, "r") or die("Could not open file!"); // read file contents $data = fread($fh, filesize($filename)) or die("Could not read file!"); // close file fclose($fh); // print file contents echo "<h3>Contents of File</h3> <form action='$_SERVER[php_self]' method= 'get' > <textarea name='newdata' cols='100%' rows='500'> $data </textarea> <input type='submit' value='Change'> </form>"; ?>

And an excerpt from a testfile.php that causes this problem:

if($_GET['edit']=='on'){

echo "

<center>

<br/><br/>

<form METHOD=post action=index.php?edit=done&page=".$page.">

  <textarea name=content cols=90 rows=40>".mysql_result($result,0,'content')."</textarea>

<br/>

<input type=submit value=Save>

</form>

</center>";

}

Thanks for reading.

    How about...

     echo "<h3>Contents of File</h3> 
    <form action='$_SERVER[php_self]' method= 'get' > 
    <textarea name='newdata' cols='100%' rows='500'>" . htmlspecialchars($data) . "</textarea> 
    <input type='submit' value='Change'> 
    </form>";
    

      thanks but that still doesn't work :glare: but why?! it should work, shouldn't it? I think any of these functions should work, there must be something else I'm missing...

      I've also tried putting the functions elsewhere in the file (for example when creating the variable that will populate the textarea) but that doesn't make any difference...

        I've got it!

        I was just putting the function in the wrong place dhoh! :p

        This is what doesn't work:

        <?
            $data = fread($fh, filesize(htmlspecialchars($filename))) or die("Could not read file!");
        ?>

        ...and this is what does work:

        <?
            $datax = fread($fh, filesize($filename)) or die("Could not read file!"); 
            $data=htmlspecialchars($datax);
        ?>

        thanks

          dameunmate;10958673 wrote:

          This is what doesn't work:

          <?
              $data = fread($fh, filesize(htmlspecialchars($filename))) or die("Could not read file!");
          ?>

          Er... why would you even expect that to work? You're trying to encode HTML entities in the name of the file before retrieving its file size... 😕

          Also note that [man]file_get_contents/man is often a simpler (and more optimized) way of reading an entire file into a variable rather than using fopen/fread/fclose.

            10 days later

            I'm sorry I don't understand the fread function, I'm just pleased that it works. Thanks

              Write a Reply...