Hi, I have the following code to edit a .txt file, but it says there is an error on line 34, I am not sure what it is. Also there are bound to be other problems with the code, so please feel free to point them out.

<? if ($_POST['pw']!="") {$pw=$_POST['pw'];}else{$pw=$_GET['pw'];}
$newcontent=$_POST['newcontent'];
$filelocation = "mytext.txt";
if (!file_exists($filelocation)) {
echo "Couldn't find datafile, please contact administrator!";
}
else {
$newfile = fopen($filelocation,"r");
$content = fread($newfile, filesize($filelocation));
fclose($newfile);
}
$content = stripslashes($content);
$content = htmlentities($content);
$pass="password";
if (!$pw || $pw != $pass){
$content = nl2br($content);
echo $content;
}
else {
if ($newcontent){
$newcontent = stripslashes($newcontent);
$newfile = fopen($filelocation,"w");
fwrite($newfile, $newcontent);
fclose($newfile);
echo 'Text was edited.<form><input type="submit" 
value="see changes" /></form>';    
}
else{
echo '<form method="post">
<textarea name="newcontent" cols="50" 
rows="15" wrap="virtual">';
echo $content;
echo "</textarea>
<input type="hidden" name="pw" 
value="'.$pass.'" />
<br /><input type="submit" value="edit" />
</form>';
}
}    

?>

Thanks

Recommended Answers

All 7 Replies

line 33 should start with echo ' (single quote, instead of double).

Thank you, I know get:

Warning: fread() [function.fread]: Length parameter must be greater than 0 in /home/content/38/8283238/html/edit.php on line 9

I am not sure what that means?

Thanks for your help

On line 33 use

echo '

or on line 37 use

";

because echo can work with both '' and "" but if ' is used at start the ' have to be used at the end and same with "".
And also I recommend to use "" coz it allows to use php variables directly without concatenation!

echo 'some text';

or

echo "some text";

Warning: fread() [function.fread]: Length parameter must be greater than 0 in /home/content/38/8283238/html/edit.php on line 9

filesize($filelocation) apparently returns false or zero. You cannot use fileread in that case.

Thank you, I know get:

Warning: fread() [function.fread]: Length parameter must be greater than 0 in /home/content/38/8283238/html/edit.php on line 9

I am not sure what that means?

Thanks for your help

There can be possible error of existence of file coz of which filesize is returning error and that error is not an integer size and the function fread(), which is expecting the size of file in int, is generating error!

on fcreate the filesize is zero
reading zero bytes gives the error
as you are reading the entire file,

Note:

If you just want to get the contents of a file into a string, use file_get_contents() as it has much better performance

replace else {$newfile = fopen($filelocation,"r");$content = fread($newfile, filesize($filelocation));fclose($newfile);} with else { $content = filegetcontents($filelocation);} far less overhead

Thanks guys, in the end I went with a program call edit-point as I decided this was all a bit above me at the moment!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.