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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Darkside
Forum Commoner
Posts: 43
Joined: Wed Oct 30, 2002 4:18 pm

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

Post 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?
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post 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);
Darkside
Forum Commoner
Posts: 43
Joined: Wed Oct 30, 2002 4:18 pm

Didnt work

Post by Darkside »

Thats a good idea but it didnt work

Parse error: parse error, unexpected T_ECHO in _gallery/gall.php on line 7
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post 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;
Post Reply