...

View Full Version : reading txt files



acorath
12-24-2003, 12: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:

count.txt
--------------
5
--------------

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:

items.php
--------------
<?php
$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);
fclose($fp);
?>
--------------

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

firepages
12-24-2003, 01: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

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum