Jump to content

load foder names into dropdown list?


blueman378

Recommended Posts

hi guys well heres my code:

<?php

echo "<form method=\"post\" action=\"{$_SERVER['PHP_SELF']}\">";
echo "<select name=\"cat\">";
  $dir = '/avatars';
while ($folders = scandir($dir))
            {
                   if ($folders != "." && $folders != "..")
            {
echo "<option value=\"$folders\">{$folders}</option>";
}
}
echo "</select></form>";
           ?>

 

and heres the output:

<form method="post" action="/webspirited/forum/avatar.php"><select name="cat"><br />

<b>Warning</b>:  scandir(/avatars) [<a href='function.scandir'>function.scandir</a>]: failed to open dir: No such file or directory in <b>/home/webspiri/public_html/test/webspirited/forum/avatar.php</b> on line <b>6</b><br />

<br />

<b>Warning</b>:  scandir() [<a href='function.scandir'>function.scandir</a>]: (errno 2): No such file or directory in <b>/home/webspiri/public_html/test/webspirited/forum/avatar.php</b> on line <b>6</b><br />

 

</select></form>

Link to comment
https://forums.phpfreaks.com/topic/97769-load-foder-names-into-dropdown-list/
Share on other sites

Your code will result in to a infinite loop , i had modifed the code this will list all the folders in a combobox

 

$dir = '/home/';  this will be considered as path from root directory

 

if you want to give relative path

give like

$dir = '../classes/';

 

 

 

echo "<form method=\"post\" action=\"{$_SERVER['PHP_SELF']}\">";

echo "<select name=\"cat\">";

$dir = '/home/';

$folders = scandir($dir);

foreach ($folders as $Fname)

            {

                if ($Fname != "." && $Fname != ".." && is_dir($dir.$Fname))

            {

               

             

             

echo "<option value=\"$Fname\">{$Fname}</option>";

}

}

echo "</select></form>";

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.