Anim9or Posted October 9, 2008 Share Posted October 9, 2008 Hi, I was wondering if sanyone could help me with the fopen tool. I'm tring to open a certain number of files, and pront out a specific line from them. However, the function only seems to work when a single number is given. If you use a variable, it will only access the first varaible number, even after the varaible is augmented. Here is the code: for ($read=0;$read<=$z;$read++) { $File2 = 'images/data/$read.txt'; $file[$read] = @fopen($File2, "r"); while($lines<3){ $line[$lines]=fgets($file[$read]); $lines++; } echo "<br>$read"; echo "<br>$File2"; echo "<br>$line[1]"; fclose($file[$read]); } Thanks! Link to comment https://forums.phpfreaks.com/topic/127759-solved-array-file-opening/ Share on other sites More sharing options...
Barand Posted October 9, 2008 Share Posted October 9, 2008 try resetting $lines to 0 before the while loop Link to comment https://forums.phpfreaks.com/topic/127759-solved-array-file-opening/#findComment-661278 Share on other sites More sharing options...
BillyBoB Posted October 9, 2008 Share Posted October 9, 2008 Could you post more code? Most of the variables aren't set in your code. Link to comment https://forums.phpfreaks.com/topic/127759-solved-array-file-opening/#findComment-661283 Share on other sites More sharing options...
kenrbnsn Posted October 9, 2008 Share Posted October 9, 2008 Variables aren't expanded when enclosed in single quotes: <?php $File2 = 'images/data/$read.txt'; ?> so you probably want <?php $File2 = "images/data/$read.txt"; ?> or <?php $File2 = 'images/data/' . $read . '.txt'; ?> Ken Link to comment https://forums.phpfreaks.com/topic/127759-solved-array-file-opening/#findComment-661291 Share on other sites More sharing options...
Anim9or Posted October 9, 2008 Author Share Posted October 9, 2008 I got it working, I'm not sure what it was exactly since it worked when i changed the while loop to s for loop, but I think it was in fact the "$lines" variable not resetting. Thanks everyone's quick responses Link to comment https://forums.phpfreaks.com/topic/127759-solved-array-file-opening/#findComment-661313 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.