469,603 Members | 2,069 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,603 developers. It's quick & easy.

counter


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
1 4164
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.

Similar topics

4 posts views Thread by Shane | last post: by
16 posts views Thread by Paul Rubin | last post: by
7 posts views Thread by JellyON | last post: by
reply views Thread by Earl Anderson | last post: by
7 posts views Thread by mistral | last post: by
reply views Thread by Trevor L. | last post: by
12 posts views Thread by devospice | last post: by
reply views Thread by guiromero | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.