PHP cannot write to a file

Problems with the Windows version of XAMPP, questions, comments, and anything related.

PHP cannot write to a file

Postby Sylvestro1945 » 07. February 2007 21:30

Since 2 days I am trying to start learning more about PHP, not so easy at 62. I installed first separate on windows XP pro SP2 Apache and PHP. It worked fine except that I cannot write to a file from the PHP script. I tried others and now XAMPP. Which really looks very nice and easy to start with. Unfotunately I still have the same problem. This is the testscript:
<?php
error_reporting(E_ALL & E_NOTICE);
$COUNT_FILE = "hitcounter.dat";
if (file_exists($COUNT_FILE)) {
$fp = fopen("$COUNT_FILE", "r+");
flock($fp, 1);
$count = fgets($fp, 4096);
$count += 1;
fseek($fp,0);
fputs($fp, $count);
if(fputs($fp, $count)) {
echo "WRITTEN";
flock($fp, 3);
fclose($fp);
}
echo "NOT written";
} else {
echo "Can't find file, check '\$file'<BR>";
}
?>
<center>This page has been viewed <b><?php echo $count; ?></b> times<br>

It is a page view counter. PHP can see the hitcounter.dat file, open it, read it, but cannot write to it. I keep getting "NOT written" and the value is not incremented. I tried through Windows file security to do something, but nothing seems to help. It is also not clear to me who is the user in case PHP or Apache is writing to the file. Searching on the web other people seem to have had the same problem, but I still have not found a solution. Can someone help please... many thanks
You can reply in German if that is more easy. ...
Sylvestro1945
 
Posts: 3
Joined: 07. February 2007 21:05

Postby Wiedmann » 07. February 2007 21:48

Well, your main problem:
You have a read only lock to a file and want write to it.
Wiedmann
AF Moderator
 
Posts: 17106
Joined: 01. February 2004 12:38
Location: Stuttgart / Germany

More specific?

Postby Sylvestro1945 » 08. February 2007 15:10

Could you be a bit more specific?
I assume there is nothing wrong with the PHP script itself?
From windows point of view, the file is not read-only, at least not for the normal users and I cannot find an Apache user.
If there is somewhere a read-only lock where do I change it?
Thanks
Sylvestro1945
 
Posts: 3
Joined: 07. February 2007 21:05

Postby Wiedmann » 08. February 2007 15:30

Could you be a bit more specific?

Well. look at this line:
Code: Select all
flock($fp, 1);

And now read the Manual about flock():
PHP-Manual wrote:To acquire a shared lock (reader), set operation to LOCK_SH (set to 1 prior to PHP 4.0.1).

--> Only read access is allowed to the locked file.

In your case, you want write to the file, you need:
PHP-Manual wrote:To acquire an exclusive lock (writer), set operation to LOCK_EX (set to 2 prior to PHP 4.0.1).

--> the result is:
Code: Select all
flock($fp, LOCK_EX);


I assume there is nothing wrong with the PHP script itself?

As I told you, the script have still other problems. Just correct this problem and you see the others ,-)
Wiedmann
AF Moderator
 
Posts: 17106
Joined: 01. February 2004 12:38
Location: Stuttgart / Germany

Works OK

Postby Sylvestro1945 » 08. February 2007 17:50

When you first mentioned it, I left out the flock functions. And funny enough it still did not work. So I was more confused. In the mean time with the flock still out and another try all of the sudden I could write to the file. Unfortunately I have no idea WHY it started working. And yes I now saw other problems.
Many thanks.
Sylvestro1945
 
Posts: 3
Joined: 07. February 2007 21:05


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 65 guests