Listing files in current directory

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
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Listing files in current directory

Post 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; 
?>
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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?
RogueTenshi
Forum Newbie
Posts: 1
Joined: Mon Apr 05, 2004 9:31 am

Post 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
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

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