We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

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

weikelbob
weikelbob asked
on
Medium Priority
251 Views
Last Modified: 2013-12-12
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!
Comment
Watch Question

Commented:
<?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>';
}

?>

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
you'll need to change the directory to the one you want

Author

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!

Commented:
I have no problems with that as long as any follow up questions relate to the initial one.

Author

Commented:
Very well, they would be.
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'))

Author

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.

Author

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?
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;
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>';
}

?>

Author

Commented:
OK.

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

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

Thanks!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.