Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Help Needed With Directory/file Listing Code Infinite Loop, Made an infinite loop but why is this so?
shadowx
post 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 unsure.gif )file and directory listing script in php as follows

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 smile.gif Its a start!

Thanks
Go to the top of the page
 
+Quote Post
jlhaslip
post 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 Icon

Group: [MODERATOR]
Posts: 3,994
Joined: 24-July 05
From: In Trouble Again... still?
Member No.: 9,787
Spam Patrol



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?
Go to the top of the page
 
+Quote Post
shadowx
post 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!

Go to the top of the page
 
+Quote Post
jlhaslip
post 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 Icon

Group: [MODERATOR]
Posts: 3,994
Joined: 24-July 05
From: In Trouble Again... still?
Member No.: 9,787
Spam Patrol



Exactly correct.
Go to the top of the page
 
+Quote Post
truefusion
post Nov 16 2006, 10:37 PM
Post #5


Ephesians 6:10-17
Group Icon

Group: [MODERATOR]
Posts: 1,893
Joined: 22-June 05
From: The World of Gentoo
Member No.: 8,528
T17 GFX Crew



Wouldn't a foreach statement work better here?

CODE
$dir = ".";
$file = scandir($dir);
foreach ($file as $key => $value){
echo $file[$key]."<br/>\n";
}
Go to the top of the page
 
+Quote Post
shadowx
post 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 smile.gif

Thanks to both of you!
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. How do you test your php code(77)
  2. Delphi & Assembly Forum(2)
  3. Web Surfing- Script Needed(2)
  4. Import From Excel File Into Mysql Database(9)
  5. Visual Basic 6.0 Help Needed(15)
  6. My Review Of Megaupload.com(25)
  7. Your First Autoit(4)
  8. Need To Shrink The File Size Of Web Image? - Photoshop Tricks(3)
  9. Runescape 2 Private Server: Code/guide 1(11)
  10. How Many Rodent Kills Have We Made So Far This Year?(5)
  11. Adsense Earning Is Just Made Easy(3)
  12. Help Needed With Excel Formulas(5)
  13. We Need More Members At Ipbgaming.com(28)
  14. What Made You Switch To Linux?(39)
  15. Php Ftp Upload Form(1)
  1. Sony Handycam(15)
  2. My 2 Best Sigs I've Ever Made(7)
  3. Add Flashing Inbox To Invisionfree Forum(2)
  4. File Sharing Hosts!(3)
  5. I Made It, But I Don't Like It(2)
  6. Made These Many Months Ago(1)
  7. Help Needed: Rewritting Uris From .htaccess(0)
  8. Internet Explorer - The Worst Standards Compatible Browser Ever Made(13)
  9. Logging Into Cpanel [resolved](13)
  10. Davinci Code(4)
  11. How To Extract The Audio From Youtube Videos(2)
  12. Create Dynamic Html/php Pages Using Simple Vb.net Code(1)
  13. An Uncrackable Code!(1)


 



- Lo-Fi Version Time is now: 6th September 2008 - 06:27 PM