• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

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!
0
weikelbob
Asked:
weikelbob
  • 5
  • 3
  • 2
  • +1
3 Solutions
 
cx323Commented:
<?php
if ($handle = opendir('C:\xampp\htdocs\test'))
{
      echo '
       <form>
       <select name="dir_files">';

      while(false !== ($file = readdir($handle)))
      {
            if($file != '.' && $file != '..')
            {
                  echo "<option value='$file'>$file</option>";
            }
      }

      closedir($handle);

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

?>
0
 
cx323Commented:
you'll need to change the directory to the one you want
0
 
weikelbobAuthor Commented:
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!
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
cx323Commented:
I have no problems with that as long as any follow up questions relate to the initial one.
0
 
weikelbobAuthor Commented:
Very well, they would be.
0
 
over_killedCommented:
in addition to what cx323 said a useful function is getcwd() it will basicly get the current directory. This is useful when you are hosting with certain hosts that have you buried in several directorys. So the handle bit would be

(this is if the directory you want to look at is called test)
if ($handle = opendir(getcwd() . '\test'))
0
 
weikelbobAuthor Commented:
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.
0
 
weikelbobAuthor Commented:
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?
0
 
Aamir SaeedCommented:
using
  if ($handle = opendir(getcwd() . '\test'))

you dont need to give the complete path as getcwd() does this and you only have to give the final directory name

for example, in my case it would be
if ($handle = opendir(getcwd() . '\test'))

please have a try by doing this to have some idea
echo getcwd();exit;
0
 
Aamir SaeedCommented:
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>';
}

?>
0
 
weikelbobAuthor Commented:
OK.

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

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

Thanks!
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now