Jump to content
xisto Community
Sign in to follow this  
shadowx

Help Needed With Directory/file Listing Code Infinite Loop Made an infinite loop but why is this so?

Recommended Posts

Hi all

ive got a small and simple (for the moment atleast :blink: )file and directory listing script in php as follows

$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 :wacko: Its a start!

Thanks

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

Wouldn't a foreach statement work better here?

$dir = ".";$file = scandir($dir);foreach ($file as $key => $value){echo $file[$key]."<br/>\n";}

Share this post


Link to post
Share on other sites

Ah truefusion, your code works perfectly. it goes through the directory once and prints out everything it finds :blink:Thanks to both of you!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.