View Full Version : reading txt files

12-24-2003, 01:50 AM
im sure this is extremely simple and im not seeing it.

what i have is a txt file with a number in it, say:


now i want to use this number as a variable in my php file. i know this next example is incorrect but its the easiest way of expressing what i want:

$total = include 'count.txt'; // so i want $total to equal 5
$newtotal = $total+1; // so i want $newtotal to equal 6
$fp = fopen('./count.txt' , 'w');
fwrite($fp, $newtotal);

so basically, i think my problem is that Im not sure exactly how to use the number in count.txt as a variable in my items.php. Any help would be greatly appreciated! Thanks :)

12-24-2003, 02:15 AM
IF your php version is >=4.3.0

$total = trim( file_get_contents( 'count.txt' ) ) ;

else (and there are several ways this is just one of them )

$file = file( 'count.txt' ) ;
$total = trim( $file[0] ) ;

the trim() is simply to get rid of the newline that may or may not exist.

... since you are already using fopen etc , you shoud also checkout the manual on fread() , fgets() & friends

12-24-2003, 02:21 AM
thanks a ton! I'll be sure to read up on those