Link to home
Start Free TrialLog in
Avatar of weikelbob
weikelbobFlag for United States of America

asked on

How to echo what's in a directory on the server.

How do I echo a list of what's in a certain folder on my server?

I want to make a dropdown list that contains the names of the files in a directory using PHP

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of cx323
cx323

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cx323
cx323

you'll need to change the directory to the one you want
Avatar of weikelbob

ASKER

Awesome.

Will it bother you if I leave this question open over the next week while I play with this?

If so, I'm happy to close it sooner.

Thanks!
I have no problems with that as long as any follow up questions relate to the initial one.
Very well, they would be.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh, so for cx323's script to work I have to run phpinfo and get the whole directory structure of my server?

Both ways sound useful.
How do I use getcwd to view a directory that is one level back?

Say I'm in an admin directory

/pages/admin/currentfile.php

and I want to make a dropdown box of the files in

/pages/music/

and only the files that have the extension .mp3

How do I do that?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sorry i think i did not read your question properly so here it goes again

i dont think that getcwd() can do this as it returns only the current working direcoty
http://php.net/getcwd

and you can try some code like as per your requirements


<?php
if ($handle = opendir('C:wamp\www\pages\music'))
{
      echo '
       <form>
       <select name="dir_files">';

      while(false !== ($file = readdir($handle)))
      {
                  $file_exp = explode(".", $file);
                  if($file_exp[1] == 'jpg')
            {
                  $my_file = $file_exp[0].'.'.$file_exp[1];
                          echo "<option value='my_$file'>my_$file</option>";
            }
      }

      closedir($handle);

      echo '
      </select>
      </form>';
}

?>
OK.

I got a dropdown box working, btw, it's great.

I'll add this code to specify only a certain extension.

Thanks!