ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> php-general
php-general
[PHP] Re: Directory to array to select box...
by Michael Virnstein other posts by this author
Apr 5 2002 3:56PM messages near this date
[PHP] Re: Directory to array to select box... | [PHP] Re: Directory to array to select box...
how about that:

<p> 
<FORM METHOD="post" ACTION="blank.php3"> 
   <SELECT NAME="files"> 

<?php
$dir = opendir("/home/web/b/bignickel.net/htdocs/");
while (false !== ($file = readdir($dir)) {
    if ($file == "." || $file == "..") {
        continue;
    }
    echo "<OPTION VALUE=\"$file\"> $file</OPTION>\n";
}
?> 

   </SELECT> 
   <INPUT TYPE="submit" NAME="submit" VALUE="select"> 
</FORM> 
</p> 

in your example there are two mistakes:
1. "while ($file = readdir($dir))" is wrong and should be "while (false !==
($file = readdir($dir))". (see php manual readdir)
2. "$file_count .="<OPTION VALUE=\"$file\"> $file</OPTION>";" is not inside
the loop,
    so you will only get the last $file and not all
(3. you probably do not need "." and ".." in your list, "." is a reference
to the same directory and ".." to the directory above the current one)

"Jas" <jlgerfen@[...].com>  schrieb im Newsbeitrag
news:20020405153411.54502.qmail@[...]..
>  Ok here is my problem, for one I am new to php and would like a new pair
of
>  eyes for this piece of code, and second what I would like to accomplish is
>  putting the contents of a directory into an array and then pulling the
>  contents of that array into a select box for further processing... If
>  someone could give me some more information on GetDirArray() or a
different
>  way to accomplish this problem that would be great... this is where I am
>  thus far.
>  <?php
> 
>  $i=0;  // counter
>  $files = array(); // array to store directory content
> 
>  // open directory
>  $dir = opendir("/home/web/b/bignickel.net/htdocs/");
> 
>  // loop through directory and put filenames into an array
>  while ($file = readdir($dir)) {
>    $files[$i] = $file;
>    $i++;
>  }
>  // pull $file (directory array into select box)
>  $file_count .="<OPTION VALUE=\"$file\">$file</OPTION>";
>  $form = "<p><FORM METHOD=\"post\" ACTION=\"blank.php3\">
>  <SELECT NAME=\"files\">$file_count</SELECT>
>  <INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"select\">
>  </FORM></p>";
>  // close directory
>  closedir($dir);
>  ?>
>  Then of course I just use an echo to display the select box but so far I
>  have not been able to have it actually work so any help would be great..
>  Thanks in advance,
>  jas
> 
> 



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Thread:
Jas
Michael Virnstein
Michael Virnstein
Jas

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved