Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
  • Register
Welcome to the Q&A for Question2Answer.

If you have questions about the platform, click here to ask and please use English.

If you just want to try Q2A, please use the demo, which also grants admin access.

Apr 29: Q2A 1.5.2

Unable to write to text file

0 votes

I am writing a hit counter plugin for Q2A. I used qa-plugin.php to register a widget module. Here is part of the code:

 

<?php
$file = fopen('hits.txt','r+') or exit('Unable to open file.');
$current = fgets($file) + 1;
rewind($file);
fwrite($file, $current) or exit('Unable to write to file.');
fclose($file);
echo $current;
?>
 
I am getting the error "Unable to open file", even though I set the permissons to 777. Any ideas?
Q2A version: 1.4.3
asked Dec 28, 2011 in Plugins by edward

1 Answer

0 votes

Why not just:

<?php
$file 
'people.txt';
// Open the file to get existing content
$current file_get_contents($file);
// Append a new person to the file
$current .= "John Smith\n";
// Write the contents back to the file
file_put_contents($file$current);
?>

?

answered Dec 29, 2011 by NoahY
Thanks for the reply Noah. The problem is I can't do anything with a file because file_exists('hits.txt') always returns FALSE even though the file is there.
check file and directory permissions and ownership