0

I'm stuck with a problem fwrite() in PHP. I'm using Raspberry Pi with running server on it Apache2. This code works perfectly on Windows. The problem is that I cannot write anything to the file. The code just erase everything what is in file and didn't write to it. I can see that the file is updated, but still 0 bytes written. All permissions to file, folder and user is set as it should be. Even logging from the root account and executing PHP code will make any sense. I Googled for 3 days and someone has the same problem as me, they are mentioning something like SElinux. All help are appreciated.

This is my PHP code:

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
if (isset($_POST['temp']))    
{    
$name = $_POST['temp'];
$fp = fopen("/var/www/smarthome/webcompare6.txt", "a");
$data = "$name";
echo ("$name");
fwrite ($fp, ($data));
sleep (1);
fclose ($fp);       
}    

?>

This is my HTML form, from which I would like to write user input to .txt file:

<form name="form" method="post" action="room6.php"> 
Temperature: <input type="text" name="temp"> 
<input type="submit" name="set" value="Set"> 
</form></th>
user2332502
  • 3
  • 1
  • 2
  • 3
    `"$name"`, `($data)` are cargo-cult programming.. – Marc B May 02 '13 at 15:44
  • 1
    why `fwrite ($fp, ($data));` and not `fwrite ($fp, $name);`? – jimmy May 02 '13 at 15:45
  • You should check the return values of both `fopen()` and `fwrite()`, are either of them returning false? Also, why `sleep(1)`? – leftclickben May 02 '13 at 15:47
  • about `sleep(1)`, I red in one forum that sometimes it can be a problem in PHP if you will not include sleep time between `fopen()` and `fwrite()`, some kind of a bug. My problem was with defining `$name` with `$data`. Now everything works perfectly when I use `fwrite ($fp, ($name))`. Thank you all for the help guys. – user2332502 May 03 '13 at 09:58
  • but the problem is that when the .txt file is completely empty (o bytes) I can not write anything to it. As long as just 1 byte is written to .txt file by hand everything works perfectly. Where can be the problem? I need that everytime old data will be deleted and new written to the file. – user2332502 May 03 '13 at 10:16

0 Answers0