counter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • [XaToA]

    #1

    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,fil esize($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.filesi ze]: 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.phpconta cto.php on
    line 19

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

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



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


  • Joshua Ghiloni

    #2
    Re: counter

    Phil Roberts wrote:[color=blue]
    > With total disregard for any kind of safety measures "[XaToA]"
    > <batarrita_NOSP AM_@excite.com> leapt forth and uttered:
    >
    >[color=green]
    >>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,fil esize($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.filesi ze]: 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.phpconta cto.php on line 19
    >>
    >>Warning: fwrite(): supplied argument is not a valid stream
    >>resource in /home/usuairo/pagina.phpconta cto.php on line 20
    >>
    >>Warning: fclose(): supplied argument is not a valid stream
    >>resource in /home/usuairo/pagina.phpconta cto.php on line 21
    >>
    >>
    >>
    >>can you help me for solving the problem and putting one counter
    >>in my php page?
    >>thanks
    >>
    >>
    >>[/color]
    >
    >
    > 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.
    >[/color]

    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.

    Comment

    Working...