0
votes

I'm trying to make a comments system to my site, but when i press the post button it gives me errors.

The errors is :

Warning: fopen(comments.txt): failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/bikereport/new design v1/Denmark.php on line 11

Warning: fread() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/bikereport/new design v1/Denmark.php on line 12

Warning: fopen(posts.txt): failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/bikereport/new design v1/Denmark.php on line 15

Warning: fwrite() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/bikereport/new design v1/Denmark.php on line 17

Warning: fclose() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/bikereport/new design v1/Denmark.php on line 18

Warning: fclose() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/bikereport/new design v1/Denmark.php on line 19

BUT!! I think all the errors are triggered by the first one, the one on line 11

My HTML form :

<form class='' action='' method='POST'>
              First and last name:<br>
              <input required class='font2 i' style='width: 262px;' type='search' name='name' value=''><br><br>

              City or state:
              <input required class='font2 i' type='search' name='city' value=''>         Phone number:<br>
              <input class='font2 i' type='search' name='phone' value=''><br><br>
              E-mail:<br>
              <input required class='font2 i' type='search' name='email' value=''>         Bike serial number:<br>
              <input class='font2 i' type='search' name='serial' value=''><br><br>
              Information about the bike:<br>
              <textarea required class='font2 info' name='info'></textarea><br>
              <input type='submit' name='Submit' value='Post'>
            </form>

My PHP code

<?php
  if($_POST){
   $name = $_POST['name'];
   $city = $_POST['city'];
   $phone = $_POST['phone'];
   $email = $_POST['email'];
   $serial = $_POST['serial'];
   $info = $_POST['info'];

   #Get old comments
   $old = fopen("posts.txt", "r+t");
   $old_comments = fread($old, 1024);

   #Delete everything, write down new and old comments
   $write = fopen("posts.txt", "w+");
   $string = "<div class='font2 post'>".$name."<br><br>".$city."<br>".$phone." 
   <br>".$email."<br><br>".$serial."<br><br>".$info."<br>\n".$old_comments;
   fwrite($write, $string);
   fclose($write);
   fclose($old);
 }
?>
3
There's no need to fopen a file twice, open it once with write permissions, get what you need and then re-write to it. - Lewis Browne
Lewis Browne - So just delete the line of code? " $old = fopen("posts.txt", "r+t") ;" - Oliver R Christensen
Please break your issue down. You want to build a comment system, yet the error you are experiencing has nothing to do with that. Please read mvce - k0pernikus

3 Answers

0
votes

There are a lot of issues with your code - not just the ones being reported as errors and warnings; I hope that you'll learn a bit about security before you consider exposing tis to any users / let alone on the internet. You might want to seek some guidance at codereview.stackexchange.com

The code you've shown us did not produce the errors and warnings you've shown us. Assuming that the code is similar...you are trying to read from a file which doesn't exist. The first time you open the file, you do so with read and write - but you never write using that file handle. You are open the file a second time without closing the first handle - MSWindows is very heavy handed with file locking - but were that not the case you'd run into nasty race conditions. Its a lot cleaner to use file_get_contents() and file_put_contents() to read and write files:

 #Get old comments
 $old_comments = file_get_contents("posts.txt");

 #Delete everything, write down new and old comments
 $string = "<div class='font2 post'>".$name."<br><br>".$city."<br>".$phone." 
   <br>".$email."<br><br>".$serial."<br><br>".$info."<br>\n".$old_comments;
 file_put_contents("posts.txt", $string);

Alternatively if you want to persist using fopen() and discover the pain for yourself, then open it once in append mode (creates the file if it does not exist) and seek (fseek($handle,0);) to the beginning before reading, and again before writing.

0
votes

I'm pretty sure you cannot open the same file twice at the same time. You need to open it a single time in read-write mode or close the file before re-opening it for writing.

0
votes

You cannot open a file twice. If you open it a second time, before closing it, it will return FALSE instead of a file handler. Hence the error message contains the message

Warning: fclose() expects parameter 1 to be resource, boolean given in ...

So, close it first and then reopen it.