You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Łukasz Hejnak <sz...@wp.pl> on 2005/11/20 14:12:18 UTC

[users@httpd] Problem with the fwrite function (not as simple as it sounds though)

Hello,
First of all sorry for my english, trying my best :]
I posted this also to PHP General, but although help, the problem is 
still not solved. Anyway I think this is more apache related than php, 
that's why I post this here.

The story is: I'm running a small webserver and to monitor the amount of 
people going trough my pages, I've made a simple counter (storing the 
data in a text file), no matter the details, the important thing is that 
it WORKED just the way I wanted it to. Till friday evening.
Yesterday I noticed that all of the counters, that were modyfied through 
that night, have been blanked. I started looking through the logs and 
found a large amount of
  PHP Warning:  fread(): Length parameter must be greater than 0.
which was the expected behaviour when reading an empty file.
After a while of checking, I got to the fact that something's wrong with 
the fwrite() function.
I've writen a 'test' script only to see if I can write files.
When writing to the /tmp dir, all went fine, but when I tried to write 
to any of the directories within my $WWWroot the result was an empty file.
This is the script:

$data="some data";
$file="/wwwroot/file"
$handle=fopen($file,"w");
fwrite($handle,$data);
fflush($handle);
fclose($handle);

And as written above, the output was only an empty "" file /wwwroot/file 
nothing more.
I thought about premissions, but after setting the wwwroot a+rwx, 
setting the owner and group as the same as the apache server works under 
I got the idea that's not it.
I reinstalled the apache and php.
Now I'm using httpd-2.1.9 and php-4.4.0 (as a module) but the problem is 
still there, before I was using httpd-2.1.8, but as I said, the problem 
appeared out of nowhere, when the server was just running on it's own, 
without upgrades, patches, config changes, or anything other.
In fact all it was doing trough the night was serving the websites and 
receiving mail.

I runned out of ideas. The logs don't show anything related to this. I 
tried to enable all debug logging, but despite the fact that I'm 
receiving now 3 times more info on everything else, there's nothing on 
the fwrite() attempt or related.

Additionaly the info we gathered is:

the fwrite is completely fine, when fopen is in 'a' mode!
Thus leading to a temporary workaround like this:

$file = "/wwwroot/file"
$countt = explode(".", fread(fopen($file,"r"), filesize($file)));
$count=$countt[1];
$count++;
$handle=fopen($file,"a");
ftruncate($handle, 1);
fwrite($handle,$file);
fclose($handle);

Now having a file with content in the form:
.15
makes it possible to use the above as a workaround..
but still I need to find out what's the problem here. The counter isn't 
exactly the only thing I use fwrite for..

The clue I got is, non-empty files are ok to be written in (appended), 
empty on the other hand, are totaly uncool for that (even using append).
So using a ftruncate($handle,0); is just as I'd use fopen($file,"w");
(gives the same result).


and another bunch of things that came out while Suhas Pharkute was 
helping me find a resolution:

So the safe-mode is off, that's for sure, I turned it off at compile stage.

I delete the file after each run of the script, the folder  where the 
script is located has 777 permissions and the ownership is given to uid 
1027 gid 1020, whereas these are the uid/gid set for apache to run with 
(and it does!).
The script:

$data="some data";
$file="file.txt";
$handle = fopen($file,"w");
fwrite($handle,$data);
fflush($handle);
fclose($handle);
if (is_writable($file))
     echo "Is writable<br>";
else
     echo "Isn't writable<br>";

works when I run it as root, from the shell (php script.php),
but when it's run from the server, it doesn't.
I mean, the output _IS_ in _BOTH_ cases "Is writable"
but that doesn't change a thing, cause when executed from the apache, it 
just creates an empty file without content.

The other thing I was supposed to check was stat($file); with 
clearstatcache(); called beforehand. The output of this is:

device number: 777
inode number: 1142470
inode protection mode: 33188
number of links: 1
_userid_of_owner:_1027_
groupid of owner: 1020
device type, if inode device *: 0
_size_in_bytes:_0_
time of last access (Unix timestamp): 1132482491
time of last modification (Unix timestamp): 1132482505
time of last inode change (Unix timestamp): 1132482505
blocksize of filesystem IO *: 4096
_number_of_blocks_allocated:_0_

and of course when executed as root:

device number: 777
inode number: 1142470
inode protection mode: 33188
number of links: 1
_userid_of_owner:_0_
groupid of owner: 1020
device type, if inode device *: 0
_size_in_bytes:_9_
time of last access (Unix timestamp): 1132482980
time of last modification (Unix timestamp): 1132482980
time of last inode change (Unix timestamp): 1132482980
blocksize of filesystem IO *: 4096
_number_of_blocks_allocated:_8_



Can anybody help?
If any more data is required on my side, I will gladly cooperate to get 
the thing solved.

--
Best wishes
Łukasz Hejnak

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org