[SOLVED] fread fwrite a file containing a <textarea> </texta

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dameunmate
Forum Newbie
Posts: 7
Joined: Wed Jul 21, 2010 2:15 am

[SOLVED] fread fwrite a file containing a <textarea> </texta

Post by dameunmate »

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.

Code: Select all

<?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:

Code: Select all

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.
Last edited by dameunmate on Wed Jul 21, 2010 2:28 pm, edited 1 time in total.
Gargoyle
Forum Contributor
Posts: 130
Joined: Wed Jul 14, 2010 12:25 am

Re: fread fwrite a file containing a <textarea> </textarea>

Post by Gargoyle »

they don't go missing, but unescaped quotes break the HTML code.

Code: Select all

$data = htmlentities($data);
dameunmate
Forum Newbie
Posts: 7
Joined: Wed Jul 21, 2010 2:15 am

Re: fread fwrite a file containing a <textarea> </textarea>

Post by dameunmate »

Yes I've got it! thanks Gargoyle!

I was just putting the function in the wrong place :crazy:

This is what doesn't work:

Code: Select all

<?
    $data = fread($fh, filesize(htmlspecialchars($filename))) or die("Could not read file!");
?>
...and this is what does work:

Code: Select all

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