|
|
|
|
![]() ![]() |
Nov 16 2006, 08:26 PM
Post
#1
|
|
|
A clever man learns from his own mistakes, a WISE man learns from those of OTHERS ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 978 Joined: 12-April 06 From: Essex, UK Member No.: 21,719 |
Hi all
ive got a small and simple (for the moment atleast CODE $dir = "."; $num = 0; $file = scandir($dir); while($file = scandir($dir)){ echo $file[$num]; echo "<BR>"; $num = $num + 1; }; the concept is simple enough, the directory to start with is the current one, so scan this directory and while we have a result do a loop to echo the file/directories found, incriment the array number by one so we get the next file and not the same one. But it seems to go on for infinity, and perhaps beyond! I think the problem is that it keeps incrementing the variable $num and then it prints out the array $file[$num] even though the value is empty. So i think the solution is how do i check how many files/directories are in the path i want to scan? And if thats not the problem then what is the problem? and how do i fix it? Other than that the script works Thanks |
|
|
|
Nov 16 2006, 09:18 PM
Post
#2
|
|
|
A computer once beat me at chess, but it was no match for me at kick boxing. ![]() Group: [MODERATOR] Posts: 3,994 Joined: 24-July 05 From: In Trouble Again... still? Member No.: 9,787 ![]() |
I think the loop is infinite due to the while conditional which asks to simply perform the scandir each time through the loop, which of course it does each and every time, so on and on it goes.
Instead, of dealing with the scandir function in the while statement, try reading the $files array based on the value of the loop index and printing something then, Might be nice to have that list clickable, too? |
|
|
|
Nov 16 2006, 09:32 PM
Post
#3
|
|
|
A clever man learns from his own mistakes, a WISE man learns from those of OTHERS ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 978 Joined: 12-April 06 From: Essex, UK Member No.: 21,719 |
QUOTE Instead, of dealing with the scandir function in the while statement, try reading the $files array based on the value of the loop index and printing something then, By this do you mean that i would put the scandir outside the loop and then use the variable $file as the loop condition and reading of the array based on the number of times the loop has run? Eg first time it would read the ) array, then the 1st, 2nd etc....? If so i think ive tried that but ill try it again in the morning (i cant concentrate now so it means its time i got some sleep!) And also the list will be clickable once ive got it to display the files/directories, that wont be a problem once ive got it to not be so infinite! |
|
|
|
Nov 16 2006, 09:37 PM
Post
#4
|
|
|
A computer once beat me at chess, but it was no match for me at kick boxing. ![]() Group: [MODERATOR] Posts: 3,994 Joined: 24-July 05 From: In Trouble Again... still? Member No.: 9,787 ![]() |
Exactly correct.
|
|
|
|
Nov 16 2006, 10:37 PM
Post
#5
|
|
|
Ephesians 6:10-17 ![]() Group: [MODERATOR] Posts: 1,893 Joined: 22-June 05 From: The World of Gentoo Member No.: 8,528 ![]() |
Wouldn't a foreach statement work better here?
CODE $dir = ".";
$file = scandir($dir); foreach ($file as $key => $value){ echo $file[$key]."<br/>\n"; } |
|
|
|
Nov 17 2006, 11:21 AM
Post
#6
|
|
|
A clever man learns from his own mistakes, a WISE man learns from those of OTHERS ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 978 Joined: 12-April 06 From: Essex, UK Member No.: 21,719 |
Ah truefusion, your code works perfectly. it goes through the directory once and prints out everything it finds
Thanks to both of you! |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 6th September 2008 - 06:27 PM |