Simple text file reading codes NOT working

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jofio

    #1

    Simple text file reading codes NOT working

    My PHP code to read a simple text file is listed below. What could have
    gone wrong here? When I run it, the browser displays: 'COULD NOT Read'
    opton of the fread() function. I thought the code was OK.


    <?php

    $visitorFile="c ounter.txt";
    $fh=fopen($visi torFile,"r+") or die("could not open.");
    echo 'Debug 1: opened to read';
    //read from it
    $Visited=fread( $fh, filesize($visit orFile)) or die("Could not
    read.");

    //Add counter to reflect new count
    $Visited ++;
    //close file
    fclose($fh);
    echo "your are visitor $Visitor";
    ?>


    Please help.


    Jofio

  • Don Freeman

    #2
    Re: Simple text file reading codes NOT working


    "Jofio" <j9k22000@yahoo .com> wrote in message
    news:1129573208 .355392.108220@ g49g2000cwa.goo glegroups.com.. .[color=blue]
    > My PHP code to read a simple text file is listed below. What could have
    > gone wrong here? When I run it, the browser displays: 'COULD NOT Read'
    > opton of the fread() function. I thought the code was OK.
    >
    >
    > <?php
    >
    > $visitorFile="c ounter.txt";
    > $fh=fopen($visi torFile,"r+") or die("could not open.");
    > echo 'Debug 1: opened to read';
    > //read from it
    > $Visited=fread( $fh, filesize($visit orFile)) or die("Could not
    > read.");
    >
    > //Add counter to reflect new count
    > $Visited ++;
    > //close file
    > fclose($fh);
    > echo "your are visitor $Visitor";
    > ?>
    >
    >[/color]

    I don't know what system you are on but for fread() the manual states:

    On systems which differentiate between binary and text files (i.e. Windows)
    the file must be opened with 'b' included in fopen() mode parameter.

    $handle = fopen($filename , "rb");

    I just use the following code for my counter:

    <?php
    $count_for_page = ("hitcounter.tx t");
    $hits = file($count_for _page);
    $hits[0]++;
    $fp = fopen($count_fo r_page, "w");
    fputs($fp, "$hits[0]");
    fclose($fp);
    echo $hits[0];
    ?>

    -Don
    --
    Ever had one of those days where you just felt like:
    http://cosmoslair.com/BadDay.html ?


    Comment

    • Ewoud Dronkert

      #3
      Re: Simple text file reading codes NOT working

      Jofio wrote:[color=blue]
      > gone wrong here? When I run it, the browser displays: 'COULD NOT Read'
      >
      > $fh=fopen($visi torFile,"r+") or die("could not open.");
      > $Visited=fread( $fh, filesize($visit orFile)) or die("Could not[/color]

      Probably has to do with the r+ mode of opening the file. Try r or rb, or
      getting the filesize before opening the file.

      --
      E. Dronkert

      Comment

      • Steve

        #4
        Re: Simple text file reading codes NOT working

        [color=blue]
        > My PHP code to read a simple text file is listed below. What could have
        > gone wrong here? When I run it, the browser displays: 'COULD NOT Read'
        > opton of the fread() function. I thought the code was OK.[/color]

        There are many possible reasons. Please ensure that error reporting is
        turned on. See
        <http://www.php.net/manual/en/function.error-reporting.php>.

        ie first statement: error_reporting (E_ALL);

        Then you should see a message along the lines of:

        PHP Warning: fopen(counter.t xt): failed to open stream: No such
        file or directory in xxxx on line x

        This will show the real reason your fopen() is failing. You might have
        to use View / Source to see the error message embedded in your HTML if
        you have already written tags.

        ---
        Steve

        Comment

        Working...