fileio in php

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

    #1

    fileio in php

    Dear all,

    I just don't know why the counter.txt just contains the last digit and so
    can't accumulate more than 9. furthermore, could anybody tell me in case
    php.net help doesn't return explanations, what else place i can go for to
    check whether I'm using the functions correctly? Thanks a lot

    $filename ="counter.tx t";

    $handle = fopen($filename , 'r');

    $content = fread($handle, filesize($filen ame));
    list ($counter) = $content;

    fclose($handle) ;
    $handle = fopen($filename , 'w');
    $counter = $counter + 1;
    fwrite($handle, $counter);
    fclose($handle) ;





  • Pedro Graca

    #2
    Re: fileio in php

    a wrote:
    I just don't know why the counter.txt just contains the last digit and so
    can't accumulate more than 9. furthermore, could anybody tell me in case
    php.net help doesn't return explanations, what else place i can go for to
    check whether I'm using the functions correctly? Thanks a lot
    >
    $filename ="counter.tx t";
    >
    $handle = fopen($filename , 'r');
    >
    $content = fread($handle, filesize($filen ame));
    $content is now a string.
    list ($counter) = $content;
    But you treat it as an array!

    What happens is that PHP takes every single character in $content as
    each element of the array.


    Try this:

    <?php
    $content = "42\n";
    list($one) = $content;
    list($two1, $two2) = $content;

    echo '$one is ', $one, "<br>\n";
    echo '$two1 is ', $two1, ' and $two2 is ', $two2, "<br>\n";
    ?>

    Anyway, $content already has the value that was in the file. If you need
    another variable to hold that value, just do

    if (is_numeric($co ntent)) {
    $counter = (int)$content;
    } else {
    $counter = 0; // or some other initialization value (1 ??)
    }

    <snip>

    --
    File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot

    Comment

    Working...