By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,304 Members | 1,254 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,304 IT Pros & Developers. It's quick & easy.

counter

P: n/a

Hello.
I am trying to put one counter in my php with the next code:
<?php

$hitfile = "/admin/contador.txt";
if (!file_exists($hitfile)) {
$file = fopen($hitfile,"r+");
$hits = fread($file,filesize($hitfile));
fclose ($file);
}
else {
$hits = 0;
}
$file = fopen($hitfile,"w+");
fwrite ($file, ++$hits);
fclose ($file);
?>

but when i try to see the php page appears this error:

Warning: fopen(/admin/contador.txt) [function.fopen]: failed to create
stream: No such file or directory in /home/usuairo/pagina.php on line 9

Warning: filesize() [function.filesize]: Stat failed for /admin/contador.txt
(errno=2 - No such file or directory) in /home/usuairo/pagina.php on line 10

Warning: fread(): supplied argument is not a valid stream resource in
/home/usuairo/pagina.php.php on line 10

Warning: fclose(): supplied argument is not a valid stream resource in
/home/usuairo/pagina.php.php on line 11

Warning: fopen(/admin/contador.txt) [function.fopen]: failed to create
stream: No such file or directory in /home/usuairo/pagina.phpcontacto.php on
line 19

Warning: fwrite(): supplied argument is not a valid stream resource in
/home/usuairo/pagina.phpcontacto.php on line 20

Warning: fclose(): supplied argument is not a valid stream resource in
/home/usuairo/pagina.phpcontacto.php on line 21

can you help me for solving the problem and putting one counter in my php
page?
thanks
Jul 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Phil Roberts wrote:
With total disregard for any kind of safety measures "[XaToA]"
<ba***************@excite.com> leapt forth and uttered:

Hello.
I am trying to put one counter in my php with the next code:
<?php

$hitfile = "/admin/contador.txt";
if (!file_exists($hitfile)) {
$file = fopen($hitfile,"r+");
$hits = fread($file,filesize($hitfile));
fclose ($file);
}
else {
$hits = 0;
}
$file = fopen($hitfile,"w+");
fwrite ($file, ++$hits);
fclose ($file);
?>

but when i try to see the php page appears this error:

Warning: fopen(/admin/contador.txt) [function.fopen]: failed to
create stream: No such file or directory in
/home/usuairo/pagina.php on line 9

Warning: filesize() [function.filesize]: Stat failed for
/admin/contador.txt (errno=2 - No such file or directory) in
/home/usuairo/pagina.php on line 10

Warning: fread(): supplied argument is not a valid stream
resource in /home/usuairo/pagina.php.php on line 10

Warning: fclose(): supplied argument is not a valid stream
resource in /home/usuairo/pagina.php.php on line 11

Warning: fopen(/admin/contador.txt) [function.fopen]: failed to
create stream: No such file or directory in
/home/usuairo/pagina.phpcontacto.php on line 19

Warning: fwrite(): supplied argument is not a valid stream
resource in /home/usuairo/pagina.phpcontacto.php on line 20

Warning: fclose(): supplied argument is not a valid stream
resource in /home/usuairo/pagina.phpcontacto.php on line 21

can you help me for solving the problem and putting one counter
in my php page?
thanks

Use ./admin not /admin. When you prefix a path with / you are
referencing the root of the filesystem rather than a path relative
to the script.


But what if /admin/contador.txt is valid? The problem I see is one of
backwards logic. If I'm reading this correctly, You should open and read
the file if it doesn't exist. How do you open and read a file that
doesn't exist? Furthermore, if the file does exist, it (as is specified
by later code) contains the number of hits. Why then, if the file
exists, should $hits = 0? I only ask that because that is what this code
is saying.

Jul 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.