Page 1 of 1

Listing files in current directory

Posted: Sat Apr 03, 2004 9:58 pm
by josh
How do I obtain a list of files in any given directory? I would need to output them to a select list for someone to choose.... I found this code elsewhere on the forums but it wouldn't output anything at all

Code: Select all

<?php 

function ListDirContents($align) 
&#123; 
   $dir_full = shell_exec("ls"); 
   $dir_files = explode("\n", $dir_full); 
   for ($i=0; $i<count($dir_files); $i++) &#123; 
      $count = $i+1; 
      ?> 
      <div align="<?php echo $align; ?>"> 
      <p><?php echo $count; ?>. <a href="http://studentcenter.compbrain.net/~pn/<?php echo $dir_files&#1111;$i]; ?>"><?php echo $dir_files&#1111;$i]; ?></a></p> 
      </div> 
      <?php 
   &#125; 
&#125; 
?>

Posted: Sat Apr 03, 2004 11:04 pm
by josh
I figured out that this does the trick:

Code: Select all

<?php foreach (glob("../$user/*") as $filename) &#123;
   echo "<option value='$filename'>$filename</option>" . "\n";
&#125; ?>


it used to glob ../$user/*.* but i want subdirectories so that chooses files as well as subdirectories... how would i change that to glob each subdirectory to?

Posted: Mon Apr 05, 2004 9:31 am
by RogueTenshi
Haha, no offence man, but the reason it produces no output is because its a function, you have to call it to execute it

Posted: Mon Apr 05, 2004 11:04 am
by vigge89
how do you know he didn't execute it?
he figured out what made it to function again, and that must mean the he executed the function, no?