Jump to content

Infinite Loop in Glob Function


ShoeLace1291

Recommended Posts

I have a page that gets all the php files in a certain folder and displays them.  It's a foreach loop, but for some reason I'm getting an infinite loop.  This is my code:

 

<?php

echo "<table align='center' cellspacing='2' cellpadding='2' border='0'>
<tr>";

$rows = 0;

foreach(glob('forums/*.php') as $szFilename){

$rows = $rows + 1;



	echo "<td style='text-align: center;'><img src='images/icon_phpfile.png' alt='PHP File'><br>$szFilename</td>";

	if($rows == {

	 echo "</tr><tr>";

}

}

echo "</tr></table>";

?>

Link to comment
https://forums.phpfreaks.com/topic/76656-infinite-loop-in-glob-function/
Share on other sites

Should be a problem here.... Try:

 

<?php

echo "<table align='center' cellspacing='2' cellpadding='2' border='0'>
<tr>";

$rows = 0;
$files = glob('forums/*.php');

foreach($files as $szFilename){

$rows = $rows + 1;



	echo "<td style='text-align: center;'><img src='images/icon_phpfile.png' alt='PHP File'><br>$szFilename</td>";

	if($rows == {

	 echo "</tr><tr>";

}

}

echo "</tr></table>";

?>

 

 

Orio

I noticed two problems with your code.

1) You should increment the counter after formating the line

2) You should test if the remainder of the $rows / 8 is 0

 

Try:

<?php

echo "<table align='center' cellspacing='2' cellpadding='2' border='0'>
<tr>";

$rows = 0;

foreach(glob('*.php') as $szFilename){
                echo "<td style='text-align: center;'><img src='images/icon_phpfile.png' alt='PHP File'><br>$szFilename</td>";
                $rows++;
                if($rows % 8 == 0)
                        echo "</tr><tr>";

}

echo "</tr></table>";

?>

 

Ken

Is there any way to display the filename without the name of the directory in front of it?  so instead of forums/index.php would display as just index.php.  Also, what arguement would I use for the first one if I only want to get the name of a directory? 

 

glob('here', GLOB_ONLYDIR);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

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.