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

Dani AI

Generated

Nice catches by and on the quoting issue. The fread() warning happens when filesize() returns 0 or false (empty file, unreadable path, or a race), and fread() requires a length > 0. Also note: using the short open tag <? will fail on many hosts unless short_open_tag is enabled. Use <?php instead.

If you want a small, safer rewrite, skip filesize()/fread() entirely and escape only on output. This pattern reads cleanly, handles empty files, locks on write, and avoids magic-quotes era stripslashes:

<?php
// Always use full tags.
$pass = 'password';
$file = __DIR__ . '/mytext.txt';

$content = is_readable($file) ? (string)file_get_contents($file) : '';

if ($_SERVER['REQUEST_METHOD'] === 'POST'
    && hash_equals($pass, (string)($_POST['pw'] ?? ''))
    && array_key_exists('newcontent', $_POST)) {
    $new = (string)$_POST['newcontent'];
    if (file_put_contents($file, $new, LOCK_EX) === false) {
        http_response_code(500);
        exit('Could not write file.');
    }
    header('Location: ' . $_SERVER['PHP_SELF']); // PRG: avoid resubmits
    exit;
}
?>
<form method="post">
  <input type="password" name="pw" placeholder="Password">
  <textarea name="newcontent" cols="50" rows="15"><?php
    echo htmlspecialchars($content, ENT_NOQUOTES, 'UTF-8');
  ?></textarea>
  <button type="submit">Save</button>
</form>

A few quick tips building on ’s suggestion to use file_get_contents():

  • Do not put the password in a hidden field; require it on submit or store an auth flag in $_SESSION after verifying.
  • Escape when rendering (htmlspecialchars), not before storing, so the file stays raw.
  • Check is_readable/is_writable and file permissions, and prefer UTF-8 consistently.
  • If you want a read-only view for non-auth users, render a <pre> with the escaped content.

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.