Connecting Tech Pros Worldwide Forums | Help | Site Map

Simple text file reading codes NOT working

Jofio
Guest
 
Posts: n/a
#1: Oct 17 '05
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="counter.txt";
$fh=fopen($visitorFile,"r+") or die("could not open.");
echo 'Debug 1: opened to read';
//read from it
$Visited=fread($fh, filesize($visitorFile)) 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
Guest
 
Posts: n/a
#2: Oct 17 '05

re: Simple text file reading codes NOT working



"Jofio" <j9k22000@yahoo.com> wrote in message
news:1129573208.355392.108220@g49g2000cwa.googlegr oups.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="counter.txt";
> $fh=fopen($visitorFile,"r+") or die("could not open.");
> echo 'Debug 1: opened to read';
> //read from it
> $Visited=fread($fh, filesize($visitorFile)) 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.txt");
$hits = file($count_for_page);
$hits[0]++;
$fp = fopen($count_for_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 ?


Ewoud Dronkert
Guest
 
Posts: n/a
#3: Oct 18 '05

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($visitorFile,"r+") or die("could not open.");
> $Visited=fread($fh, filesize($visitorFile)) 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
Steve
Guest
 
Posts: n/a
#4: Oct 18 '05

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.txt): 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

Closed Thread