Page 1 of 1

while ($file = readdir($dir_handle)) {

Posted: Fri Feb 21, 2003 3:29 pm
by Darkside
I used a 'Show Whats in a Directory' tutorail to help me make this code:

Code: Select all

<?
$path = "Images";
$dir_handle = @opendir($path) or die("Unable to open $path");
echo "Directory Listing of $path<BR>";
while ($file = readdir($dir_handle)) &#123;
        echo "<a href=$path/$file>$file<br><img src=$path/$file border=0></a><br>";

&#125;
closedir($dir_handle);
?>
The above code shows all the images in the directory.
This is only 1 step into what I hope to be a bigger code but I 1 down fall to this script is the 'Parent Directory' and the '$path' are shown at '.' and '..'
That in its self is not a problem but it seem it also add an image next too it.
Is there a way i can change the first two paths? i cant think of a way around this?

Posted: Fri Feb 21, 2003 5:51 pm
by lazy_yogi

Code: Select all

$path = "Images"; 
    $dir_handle = @opendir($path) or die("Unable to open $path"); 
    echo "Directory Listing of $path<BR>"; 
    while ($file = readdir($dir_handle)) { 
        if ($file == "." || $file == "..") continue   // this line added 
        echo "<a href=$path/$file>$file<br><img src=$path/$file border=0></a><br>"; 
    } 
    closedir($dir_handle);

Didnt work

Posted: Fri Feb 21, 2003 6:11 pm
by Darkside
Thats a good idea but it didnt work

Parse error: parse error, unexpected T_ECHO in _gallery/gall.php on line 7

Posted: Fri Feb 21, 2003 9:59 pm
by lazy_yogi

Code: Select all

$path = "Images"; 
    $dir_handle = @opendir($path) or die("Unable to open $path"); 
    echo "Directory Listing of $path<BR>"; 
    while ($file = readdir($dir_handle)) { 
        if ($file == "." || $file == "..") continue;   // this line added 
        echo "<a href=$path/$file>$file<br><img src=$path/$file border=0></a><br>"; 
    } 
    closedir($dir_handle);
oh .. forgot the semi colon on line 6 after the word continue;