Re: Quick TXT document stuff

From: Date: Thu, 30 Aug 2001 21:22:34 +0000
Subject: Re: Quick TXT document stuff
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-65376@lists.php.net to get a copy of this message
At 1:31 PM -0700 8/30/01, Kyle Smith wrote:
ok i have this code to do whats below, but its not working <?php $fp = fopen("meh.txt","r"); for($i=0;$i<2;$i++) { print "$fp[$i]"; } ?>
That's because $fp is a file POINTER, not the actual contents of the file. You might have been thinking of the file() function, which sucks the entire contents of the file into an array...very wasteful, if all you need are the first few lines. What you want here is: $fp = fopen("meh.txt","r"); for($i=0;$i<2;$i++) { print fread($fp, 4096); } or unset($FileArray); $fp = fopen("meh.txt","r"); for($i=0;$i<2;$i++) { $FileArray[] = fread($fp, 4096); } foreach($FileArray as $line) { print $line; } The second parameter in the fread function is the maximum line length; normally, that would be set to ma number larger than the maximum line length you're likely to encounter (unless you have long lines, and only need the first few characters of that line). See: http://www.php.net/manual/en/function.fopen.php and http://www.php.net/manual/en/function.fread.php Also, the line print "$fp[$i]"; isn't likely to work the way you expect; if $fp was an array, you'd get something like Array[0] Array[1] Array[2] as the printout, since the $i, in quotes, wouldn't be interpreted as an array index. Use something like print $fp[$i]; or print "Some text here ".$fp[$i]." more text here"; instead.
-lk6- http://www.StupeedStudios.f2s.com Home of the burning lego man! ICQ: 115852509 MSN: dbzno1fan@hotmail.com AIM: legokiller666
-- +------------------------ Open source questions? ------------------------+ | Steve Edberg University of California, Davis | | sbedberg@ucdavis.edu Computer Consultant | | http://aesric.ucdavis.edu/ http://pgfsun.ucdavis.edu/ | +----------- http://pgfsun.ucdavis.edu/open-source-tools.html -----------+

« previous php.general (#65376) next »