fileio in php

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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 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.txt";

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

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

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

Re: fileio in php

a wrote:
Quoted text here. Click to load it

$content is now a string.

Quoted text here. Click to load it

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:

    $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($content)) {
      $counter = (int)$content;
    } else {
      $counter = 0; // or some other initialization value (1 ??)


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

Site Timeline