0

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

4
Contributors
7
Replies
8
Views
5 Years
Discussion Span
Last Post by gilgil2
0

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

0

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";

Edited by pankaj_patel: further clarification

0

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.

0

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!

0

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

Edited by almostbob: n/a

0

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!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.